WHERE选择上的MySQL语法错误

时间:2015-06-02 14:56:29

标签: mysql

我正在尝试将多个表选择为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 ='

附近使用正确的语法

请有人可以指导我做错的事吗?

3 个答案:

答案 0 :(得分:3)

您在WHEREGROUP 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)

您收到此错误是因为WHERE子句应该出现在GROUP BY子句之前,正如其他人所提到的那样。我还想添加对SELECT语法的文档的引用,该语法显示了语句的子句顺序。我已经为您总结了订单,并在可选子句中加上括号:

SELECT
FROM
[WHERE]
[GROUP BY]
[HAVING]
[ORDER BY]
[LIMIT]

在旁注中,执行子句的顺序与写入的顺序不同。有关详细信息,请参阅此question