1

Let's consider 2 tables "schools" and "students". Now a student may belong to different schools in his life, and a school have many students. So this is a many to many example. A third table "links" specify the relation between student and school.

Now to query this I do the following:

Select sc.sid , -- stands for school id
       st.uid,  -- stands for student id
       sc.sname, -- stands for school name
       st.uname, -- stands for student name
       -- select more data about the student joining other tables for that
from students s
left join links l on l.uid=st.uid  -- l.uid stands for the student id on the links table
left join schools sc on sc.sid=l.sid -- l.sid is the id of the school in the links table
where st.uid=3 -- 3 is an example

this query will return duplicate data for the user id if he has more than one school, so to fix this I added group by st.uid, yet I also need the list of school name related to the same user. Is there a way to do it with fixing the query I wrote instead of having 2 queries? So as example I want to have Luci of schools ( X, Y, Z, R, ...) etc

1
  • my solution was a merge of both Ronnis and Spiny Norman. I make it that way `GROUP_CONCAT((concat(sc.sid,'=',sc.sname) SEPARATOR ', ') as school_obj``
    – Luci
    Jan 7, 2011 at 10:01

2 Answers 2

4

You can use the GROUP_CONCAT aggregate function: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat.

Like this:

Select st.uid,  -- stands for student id
       st.uname, -- stands for student name
       GROUP_CONCAT sc.sname SEPARATOR ', ' as school_names,
       -- select more data about the student joining other tables for that
from students s
left join links l on l.uid=st.uid  -- l.uid stands for the student id on the links table
left join schools sc on sc.sid=l.sid -- l.sid is the id of the school in the links table
where st.uid=3 -- 3 is an example
group by st.uid
1
  • sounds interesting, I noticed that you added the group by on the wrong position. It should be after the where. yet your methods really helped. Thanks.
    – Luci
    Jan 7, 2011 at 9:59
1

Ugly, but works.

select st.uid
      ,st.uname
      ,group_concat(concat(sc.sid,'=',sc.sname)) as example1
      ,group_concat(sc.sid)                      as example2
      ,group_concat(sc.sname)                    as example3
  from students     st
  left join links    l on l.uid  = st.uid
  left join schools sc on sc.sid = l.sid
 where st.uid = 3
 group 
    by st.uid
      ,st.uname;
  • example_1 gives you value pairs, such as (1=Cambridge,2=Oxford,3=Haganässkolan).
  • example_2 contains a csv string of school ids (1,2,3)
  • example_3 contains a csv string of school names (Cambridge,Oxford,Haganässkolan)
1
  • i like this part concat(sc.sid,'=',sc.sname)
    – Luci
    Jan 7, 2011 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.