如何在MySQL中的另一个表中显示具有多个值的列的值

时间:2016-06-13 07:50:28

标签: mysql

我有一张桌子:

enter image description here



然后B表:

enter image description here



最后一个,C表:

enter image description here


我需要证明他们是这样的:

performance_id | quiz_id
________________________
22             | 65
23             | null
24             | 43
25             | null

我尝试加入,但显示错误的结果。它不显示测验ID。我试过这个:

SELECT A.performance_id, C.quiz_id 
FROM A 
LEFT JOIN B ON A.performance_id=B.performance_id 
LEFT JOIN C ON B.phc_id = C.phc_id 
group BY A.performance_id;

结果:
enter image description here
帮助我,谢谢

2 个答案:

答案 0 :(得分:1)

使用group_concat

SELECT A.performance_id, group_concat(C.quiz_id) 
FROM A 
LEFT JOIN B ON A.performance_id=B.performance_id 
LEFT JOIN C ON B.phc_id = C.phc_id 
group BY A.performance_id;

因为您获得了多个quiz_id

答案 1 :(得分:0)

试试这个

SELECT A.performance_id, group_concat(C.quiz_id) 
FROM A 
LEFT JOIN B ON A.performance_id = B.performance_id 
LEFT JOIN C ON B.phc_id = C.phc_id 
group BY A.performance_id;

SQL Fiddle