group by - How to avoid group_concat separator duplicate in mysql select result -


i have 2 tables call record , user, fields below

record fields:

  • recordid (key)
  • userid (key)

user fields:

  • userid (key)
  • username
  • usernote

and want know recordid, username, usernote group recordid here sql

 select record.recordid, group_concat(user.username) usernames , group_concat(user.usernote) usernotes record   left outer join user on record.userid = user.userid    group record.recordid  

now can split usernames , usernotes ',' know every username , usernote

but if there ',' in usernote ,it might make mistakes

for example, ben usernote nice,tall , tom's usernote nice

the sql result be

usernames-> ben,tom

usernotes-> nice,tall,nice

in case usernotes have 3 notes, can't differ 1 tom's note

is there way avoid situation?

or best way can use separator hard appear, '!@#@!'

in real case, have 10 tables related key, if left outer join tables, tons of duplicate rows,

one method different separator:

select r.recordid,        group_concat(u.username separator ';') usernames ,           group_concat(u.usernote separator ';') usernotes record r left outer join      user u      on r.userid = u.userid   group r.recordid ; 

Comments