将2个SQL语句合并为1

时间:2016-10-10 20:23:55

标签: mysql sql

我想选择收入高于经理人的员工。我有下面写的这些SQL语句,但是我究竟如何将它们组合成一个语句呢?

SELECT E.Salary
FROM Employee E
WHERE E.ManagerId = E.Id

SELECT *
FROM Employee M
WHERE M.Salary > E.Salary AND M.ManagerId != M.Id

3 个答案:

答案 0 :(得分:3)

//

答案 1 :(得分:2)

你可以使用自我内部联接

SELECT E.*
FROM Employee E
INNER JOIN Employee M ON  E.ManagerId = M.Id
WHERE E.Salary > M.Salary

答案 2 :(得分:1)

了解加入,例如:

SELECT E.* 
FROM Employee E 
    JOIN Employee M ON E.ManagerId = M.Id 
WHERE E.Salary>M.Salary;