所以,在检查了多个论坛和类似的问题,我没有设法找到一个正是我要找的。
我有两张table:
questions answers
+-----+---------+ +------+------+---------+
| id_q| question| | id_q | id_a | answer |
+=====+=========+ +======+======+=========+
| 1 |question1| | 1 | 1 | answer1 |
+-----+---------+ +------+------+---------+
| 2 |question2| | 1 | 2 | answer2 |
+-----+---------+ +------+------+---------+
| 2 | 3 | answer3 |
+------+------+---------+
| 2 | 4 | answer4 |
+------+------+---------+
想得到这样的东西:
+-----+---------+---------+---------+
| id_q| question| answer | answer2 |
+=====+=========+=========+=========+
| 1 |question1| answer1 | answer2 |
+-----+---------+---------+---------+
| 2 |question2| answer3 | answer4 |
+-----+---------+---------+---------+
已尝试:
SELECT questions.*,
GROUP_CONCAT(answers.answer SEPARATOR ' ; ') AS answers
FROM questions
LEFT OUTER JOIN answers ON questions.id_q = answers.id_q
GROUP BY questions.id_q
但这给了我:
+-----+---------+-------------------+
| id_q| question| answers |
+=====+=========+===================+
| 1 |question1| answer1 ; answer2 |
+-----+---------+-------------------+
| 2 |question2| answer3 ; answer4 |
+-----+---------+-------------------+
不知道是否有可能实现我所尝试的。有什么想法吗?
提前谢谢。
3条答案
按热度按时间v7pvogib1#
vnjpjtjt2#
你可以用
correlated subqueries
以…的贡献mod
功能:rextester演示
vktxenjb3#
您只需自行加入答案表: