简体   繁体   English

我的 MySQL 查询有什么问题?

[英]What is wrong with my MySQL query?

SELECT E.id_employee,E.name,E.age,E.wage,D.name,
    CASE (SELECT COUNT(*) FROM manages M 
        WHERE M.id_employee=E.id_employee) 
        WHEN 1 THEN 'Chief' 
        WHEN 0 THEN '-'
    END CASE
FROM Employee E 
INNER JOIN work_in 
INNER JOIN Department D

it gives that error:它给出了那个错误:

Could not connect: You have an error in your SQL syntax;无法连接:您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE FROM Employee E INNER JOIN work_in INNER JOIN Department D' at line 5检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 5 行的“CASE FROM Employee E INNER JOIN work_in INNER JOIN Department D”附近使用

What am I missing here?我在这里想念什么? Please immediate help.请立即帮助。

Remove the last CASE keyword, after the END删除最后一个 CASE 关键字,在 END 之后

There is no 'CASE' after 'END' 'END'之后没有'CASE'

SELECT E.id_employee,E.name,E.age,E.wage,D.name,
                CASE (SELECT COUNT(*) FROM manages M WHERE M.id_employee=E.id_employee) 
                    WHEN 1 THEN 'Chief' 
                    WHEN 0 THEN '-'
                END
FROM Employee E INNER JOIN work_in INNER JOIN Department D

Remove the word CASE from after END.从 END 之后删除单词 CASE。

You're mixing up the syntax of the CASE Statement and the CASE expression .您正在混淆CASE语句CASE表达式的语法。 While the first one is used in stored procedures, the second one is the one used in SQL statements.第一个用于存储过程,第二个用于 SQL 语句中。

CASE expression CASE表达式

SELECT CASE 1 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END;

CASE statement CASE声明

CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

Or as the manual puts it或如手册所说

Note笔记

The syntax of the CASE statement used inside stored programs differs slightly from that of the SQL CASE expression described in Section 11.4, “Control Flow Functions”.存储程序中使用的 CASE 语句的语法与第 11.4 节“控制流函数”中描述的 SQL CASE 表达式的语法略有不同。 The CASE statement cannot have an ELSE NULL clause, and it is terminated with END CASE instead of END. CASE 语句不能有 ELSE NULL 子句,它以 END CASE 而不是 END 终止。

So in your case there is no CASE after the END .因此,在您的情况下, END之后没有CASE

You are missing ON condition for the inner join or USING() clause.您缺少内部联接USING()子句的ON条件。

SELECT E.id_employee,E.name,E.age,E.wage,D.name,
                CASE (SELECT COUNT(*) FROM manages M WHERE M.id_employee=E.id_employee) 
                    WHEN 1 THEN 'Chief' 
                    WHEN 0 THEN '-'
                END
            FROM Employee E 
               INNER JOIN work_in w
                  ON e.id_employee=w.id.employee
               INNER JOIN Department D
                  ON w.id_department=D.id_department

Change it to:将其更改为:

"SELECT E.id_employee,E.name,E.age,E.wage,D.name,
  CASE (SELECT COUNT(*) FROM manages M WHERE M.id_employee=E.id_employee)
    WHEN 1 THEN 'Chief'                     
    WHEN 0 THEN '-'                 
  END              
FROM Employee E 
INNER JOIN work_in w ON (w.id = e.work_id)
INNER JOIN Department D ON (D.id = e.department_id)"

Warning警告
If you do an inner join with no condition(s), it will actually transform into a full outer join !如果你做一个没有条件的inner join ,它实际上会变成一个full outer join
This means if you run the following query:这意味着如果您运行以下查询:

SELECT * FROM a
INNER JOIN b
INNER JOIN c

where table a, b and c each have 100 rows, you will get a grand total of 1,000,000 rows in your resultset, listing every possible combination of a, b and c.其中表 a、b 和 c 各有 100 行,您将在结果集中获得总共 1,000,000 行,列出 a、b 和 c 的所有可能组合。
In 99,999% of the cases this is definitely not what you'd want.在 99,999% 的情况下,这绝对不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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