LEFT JOIN,查询中不存在LIMIT

时间:2016-03-12 20:48:33

标签: php mysql join left-join inner-join

我有以下表格:

轮询

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" }}

如何在主查询中使用LIMITLIMIT上没有LEFT JOIN

1 个答案:

答案 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;