我有以下表格:
轮询:
id | question | votes
1 | Your name? | 2
poll_answers :
id | id_poll | answer
1 | 1 | John
2 | 1 | Jorge
3 | 1 | Charlie
我想要SELECT
返回poll
数据,poll_answers
数据与id_poll
对应id
poll
} table。
所以,我做了:
SELECT question, votes, answer
FROM poll
LEFT JOIN poll_answers
ON poll.id = id_poll
WHERE poll.id = '1'
LIMIT 1
预期输出将返回poll
数据和poll_answers
表上的三个答案,但输出为:
array(1){[0] => array(3){[" question"] => string(10)"你的名字?" ["票"] =>字符串(1)" 2" ["答案"] => string(5)" Charlie" }}
我想要的是什么:
array(1){[0] => array(3){[" question"] => string(10)"你的名字?" ["票"] =>字符串(1)" 2" ["答案"] =>字符串(7)"查理" ["答案"] => string(5)" Jorge" ["答案"] => string(4)" John" }}
如何在主查询中使用LIMIT
但LIMIT
上没有LEFT JOIN
?
答案 0 :(得分:1)
我认为你想聚合:
SELECT p.question, p.votes, GROUP_CONCAT(pa.answer) as answer
FROM poll p LEFT JOIN
poll_answers pa
ON p.id = pa.id_poll
WHERE p.id = 1
GROUP BY p.id;