How to use distinct inside CONCAT_WS() in MySQL?
18:34 19 Apr 2021

In mySQL I have a table:

id name code1 code2 code3
1 Jim aaa aaa a
2 Ryan bb bbb b
3 Ted c c cc

Expected output:

id name concat_code
1 Jim aaa/a
2 Ryan bb/bbb/b
3 Ted c/cc

I tried the below query:

SELECT
  id,
  name,
  CONCAT_WS("/",NULLIF(code1,""),NULLIF(code2,""),NULLIF(code3,"")) as concat_code
FROM
  table1
GROUP BY id  

But it gives wrong output:

id name concat_code
1 Jim aaa/aaa/a
2 Ryan bb/bbb/b
3 Ted c/c/cc

How can I use the distinct combination inside CONCAT_WS()?

mysql mysql-workbench