Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-mysql分组结果_Php_Mysql - Fatal编程技术网

php-mysql分组结果

php-mysql分组结果,php,mysql,Php,Mysql,有人能帮我吗? 我有两个表,它们由学生id链接。 我正在寻找一个更清晰/单一的查询,可以输出下面的示例, 尽管我通过插入另一个查询(主题为group_concat)实现了这一点 在while循环中查询节和名称 表_学生 学生id、部门、姓名 表_受试者 学生证,科目 现在我希望输出是这样的 student_id section name subject 100 A john algebra, trigo, geometry 1

有人能帮我吗?
我有两个表,它们由学生id链接。
我正在寻找一个更清晰/单一的查询,可以输出下面的示例,
尽管我通过插入另一个查询(主题为group_concat)实现了这一点
在while循环中查询节和名称

表_学生
学生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.....  
尝试以下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
你应该做你需要的事。在两个表中按学生id选择相关数据,然后指定要返回的字段。最后,按节对它们进行分组。

尝试以下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
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

你应该做你需要的事。在两个表中按学生id选择相关数据,然后指定要返回的字段。最后,按部分对它们进行分组。

对不起,必须进行编辑。我想这就是你要找的。谢谢你的快速回复。对不起,我得编辑一下。我想这就是你要找的。感谢所有的快速回复。嗨,JPR,你的答案很好,我刚刚添加了“作为subs”选择stud.section,stud.name,group_concat(subc.subject.),作为表_students stud中的subs,谢谢。在stud.student\u id=subc.student\u id分组BY stud.namehi JPR,你的答案很好,我只需添加“as subs”选项,选择stud.section、stud.name、GROUP\u concat(subc.subject.),作为表\u student stud中的subs,谢谢。通过stud.name连接stud.student\u id=subc.student\u id组上的表
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