[英]sql error 1064 when i change inner join to left join
i write sql 我写sql
SELECT
count(*) as count,g.name
FROM
test_scale g
INNER JOIN
test_user_scale_result a
INNER JOIN
en_org_user_department b
INNER JOIN
en_org_department d
INNER JOIN
test_scale_type e ON a.user_id = b.user_id
AND g.id = a.scale_id
AND b.department_id = d.id
AND d.position LIKE CONCAT((SELECT
position
FROM
en_org_department
WHERE
id = 8),
'%')
AND e.id = g.scale_type_id
and b.status = 1
and g.scale_type_id IN (1 , 9)
and a.create_time BETWEEN '2015-01-07 18:09:45' and '2015-11-09 18:09:45'
group by a.scale_id;
i run correctly. 我运行正常。 but when i change inner to left,like
但是当我从左向内改变时
SELECT
count(*) as count,g.name
FROM
test_scale g
left JOIN
test_user_scale_result a
left JOIN
en_org_user_department b
left JOIN
en_org_department d
left JOIN
test_scale_type e ON a.user_id = b.user_id
AND g.id = a.scale_id
AND b.department_id = d.id
AND d.position LIKE CONCAT((SELECT
position
FROM
en_org_department
WHERE
id = 8),
'%')
AND e.id = g.scale_type_id
and b.status = 1
and g.scale_type_id IN (1 , 9)
and a.create_time BETWEEN '2015-01-07 18:09:45' and '2015-11-09 18:09:45'
group by a.scale_id;
it error 20:37:15 SELECT * FROM test_user_scale_result a
它错误20:37:15 SELECT * FROM test_user_scale_result a
left JOIN en_org_user_department b left JOIN左JOIN en_org_user_department b左JOIN
en_org_department d left JOIN test_scale_type e left JOIN test_scale g ON a.user_id = b.user_id AND g.id = a.scale_id AND b.department_id = d.id AND d.position LIKE CONCAT((SELECT position FROM en_org_department WHERE id = 8),en_org_department d左侧JOIN test_scale_type e左侧JOIN test_scale g ON ),
'%') AND e.id = g.scale_type_id and b.status = 1'%')AND e.id = g.scale_type_id和b.status = 1
and g.scale_type_id IN (1 , 9) and a.create_time BETWEEN '2015-01-07 18:09:45' and '2015-11-09 18:09:45' Error Code: 1064. You have an error in your SQL syntax;和g.scale_type_id IN(1、9)和a.create_time BETWEEN'2015-01-07 18:09:45'和'2015-11-09 18:09:45'错误代码:1064。您的SQL语法; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 26 0.0060 sec
查看与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第26行0.0060秒处在''附近使用
I think left join
requires an on
clause immediately following the join. 我认为
left join
on
联接之后需要on
子句。 Really, you should always be doing this anyway: 确实,无论如何,您应该始终这样做:
SELECT
count(*) as count,g.name
FROM
test_scale g left JOIN
test_user_scale_result a
ON g.id = a.scale_id AND a.create_time BETWEEN '2015-01-07 18:09:45' and '2015-11-09 18:09:45' left JOIN
en_org_user_department b
ON a.user_id = b.user_id AND b.status = 1 left JOIN
en_org_department d
ON b.department_id = d.id AND
d.position LIKE CONCAT((SELECT position FROM en_org_department WHERE id = 8),
'%') left JOIN
test_scale_type e
ON e.id = g.scale_type_id
WHERE g.scale_type_id IN (1 , 9)
group by a.scale_id;
Note: 注意:
ON
clause where that table first appears. ON
子句中。 I don't think the left join
will work if all of them are stuffed in the final on
clause. on
子句中,则left join
将不起作用。 where
clause. where
子句中。 Otherwise, you'll get strange results. ON
clause follow every JOIN
, except for a CROSS JOIN
. CROSS JOIN
之外,始终在每个JOIN
都有一个ON
子句。 (2) Never use commas in the FROM
clause (you aren't doing this, but I always throw this in). FROM
子句中使用逗号(您没有这样做,但我总是将其放入其中)。 You should put the join conditions in the proper ON
clauses, not all together in one final ON
, like this: 您应该将连接条件放在适当的
ON
子句中,而不是全部放在一个最终的ON
,如下所示:
SELECT count(*) as count,
g.name
FROM test_scale g
LEFT JOIN test_user_scale_result a
ON g.id = a.scale_id
AND a.create_time BETWEEN '2015-01-07 18:09:45' and '2015-11-09 18:09:45'
LEFT JOIN en_org_user_department b
ON a.user_id = b.user_id
AND b.status = 1
LEFT JOIN en_org_department d
ON b.department_id = d.id
AND d.position LIKE CONCAT(
(SELECT position
FROM en_org_department
WHERE id = 8),
'%')
LEFT JOIN test_scale_type e
ON e.id = g.scale_type_id
WHERE g.scale_type_id IN (1, 9)
GROUP BY g.name;
The condition on the g.scale_type_id
should appear in the WHERE
clause, unless you really want to have other records included. 除非您确实希望包含其他记录,否则
g.scale_type_id
上的g.scale_type_id
应出现在WHERE
子句中。
Note also, that it is advised to group by g.name, since that is the column you SELECT
for having the related count. 还要注意,建议按g.name分组,因为这是您
SELECT
的具有相关计数的列。 In Standard SQL
it is mandatory to GROUP BY
the non-aggregated columns that you have in your SELECT
list. 在Standard
SQL
,必须对SELECT
列表中的未聚合列进行GROUP BY
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.