List of all common values ​​of second column followed by unique name in first value in mysql

I am trying to represent this specific condition. Think about the patient visiting the hospital for regular checkups. First he visits internal medicine, next to ENT, ORTHOPEDIAC and pharmacy; another time when he visits the hospital, he visits internal medicine, laboratory and ENT and pharmacy. Each patient visits certain branches during hospital visits.


PatientName Department
Alice       INTERNAL
Alice       ENT
Alice       ORTHO
Alice       PHARMACY
Bob         Internal
Bob         ORTHO
Bob         PHARMACY

      

Now How to get departments in cvs according to department traveling by unique users in this form, for example

INTERNAL, ENT, ORTHO, PHARMACY
INTERNAL, ORTHO, PHARMACY
SKIN, EYE, OPERATION, PHARMACY
INTERNAL, , , 
...
...
MEDICAL, NEURO, PSYCH, PHARMACY
...
...

      

What is the set of rules, i.e. sql queries will provide this output as tables? I can convert it to csv after downloading it.

+3


source to share


1 answer


You can use group_concat

to get a comma separated list of sections for each user.

select PatientName, group_concat(Department)
from mytable
group by PatientName

      



If you want the departments to appear in a specific order then the user is order by

insidegroup_concat

 group_concat(department order by department)

      

+2


source







All Articles