我正在尝试将多个表选择为ID = variabile。
示例代码如下,我相信它应该可以工作但不知何故我在语法中有一些错误。
SELECT c.id, c.firstname, c.surname, c.email, c.process, c.search_work, c.note,
group_concat(DISTINCT ce.enforcement) as enfor,
group_concat(DISTINCT cc.city) as city
FROM candidates AS c
LEFT JOIN candidates_language AS cl ON c.id = cl.candidates_id
LEFT JOIN candidates_enforcement as ce on c.id = ce.candidates_id
LEFT JOIN candidates_city as cc on c.id = cc.candidates_id
GROUP BY c.id, c.firstname, c.surname, c.email
WHERE c.id='8'
小提琴: http://sqlfiddle.com/#!9/25b1b/24
错误: 您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'WHERE id ='
附近使用正确的语法请有人可以指导我做错的事吗?
答案 0 :(得分:3)
您在WHERE
和GROUP BY
子句之间的位置不匹配。 WHERE
子句应始终位于GROUP BY
子句之前,因为WHERE
子句将根据条件过滤数据集,并在过滤的数据集上过滤分组或GROUP BY
将要发生。所以在你的查询中它应该是
WHERE c.id='8'
GROUP BY c.id, c.firstname, c.surname, c.email
答案 1 :(得分:0)
这就是你要找的东西:
SELECT c.id, c.firstname, c.surname, c.email, c.process, c.search_work, c.note,
group_concat(DISTINCT ce.enforcement) as enfor,
group_concat(DISTINCT cc.city) as city
FROM candidates AS c
LEFT JOIN candidates_language AS cl ON c.id = cl.candidates_id
LEFT JOIN candidates_enforcement as ce on c.id = ce.candidates_id
LEFT JOIN candidates_city as cc on c.id = cc.candidates_id
WHERE c.id='8' --where comes before the group by
GROUP BY c.id, c.firstname, c.surname, c.email;
答案 2 :(得分:0)