2012-11-29 92 views
0

任何人都可以帮到我吗?
我有两张表,它们由student_id链接。
即时寻找,以下可以输出样本,
虽然我通过插入另一个查询(具有GROUP_CONCAT用于受试者)
的部分和名称while循环查询中实现了这一更清洁剂/单个查询。php mysql分组结果

table_students
student_id数据,节名

table_subjects
student_id数据,受

现在我想的输出是这样。

student_id section name subject 
100   A   john algebra, trigo, geometry 
101   A   peter trigo, geometry, 
102   B   alice literature, algebra 
103   B   james trigo 

在此先感谢您。

对了,我忘了给一些更多的细节,在我的臣民表 ,受试者是每行,像这样

student_id subject 
    100  algebra 
    100  tigo 
    100  geometry 
    101  trigo 
    101  geometry 
    102  literature 
and so on..... 
+0

对不起,不得不作出编辑。我想这就是你要找的。 – JPR

+0

感谢所有的快速反应.. –

回答

4
SELECT 
stud.section, stud.name, group_concat(subj.subject, '') 
FROM table_students stud 
JOIN table_subjects subj 
ON stud.student_id = subj.student_id 
GROUP BY stud.name 
+0

嗨JPR,你的答案是非常好的工作,我只需要添加“作为潜艇”SELECT stud.section,stud.name,group_concat(subj.subject,'')as subs FROM table_students stud,谢谢。 JOIN table_subjects subj ON stud.student_id = subj.student_id GROUP BY stud.name –

+0

很高兴能提供帮助。 – JPR

0

试试这个MySQL查询:

SELECT stu.section, stu.name, sub.subject 
FROM table_students stu, table_subject sub 
WHERE stu.student_id=sub.student_id 
GROUP BY stu.section 

应该做你需要的东西。在这两个表中通过student_id选择相关数据,然后指定要返回的字段。最后,按部分分组。