简体   繁体   English

当我将内部联接更改为左联接时,SQL错误1064

[英]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: 注意:

  • I'm pretty sure the conditions on the particular tables need to go in the 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将不起作用。
  • The conditions on the first table should be in a where clause. 第一个表上的条件应放在where子句中。 Otherwise, you'll get strange results. 否则,您将得到奇怪的结果。
  • Here are some simple rules when using joins. 这是使用联接时的一些简单规则。 (1) Always have an ON clause follow every JOIN , except for a CROSS JOIN . (1)除CROSS JOIN之外,始终在每个JOIN都有一个ON子句。 (2) Never use commas in the FROM clause (you aren't doing this, but I always throw this in). (2)切勿在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM