首页 > Mysql 单个表查询问题

Mysql 单个表查询问题

+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+

Employee表中,ManagerId为空的是经理,找出薪水高于经理的雇员名字。

我使用:

select s.Name from Employee s join Employee i
  on s.ManagerId = i.Id
    where s.Salary > i.Salary;

这样写,有更好的写法吗?


select name from Employee t where 
    t.ManagerId is NOT NULL 
    AND 
    exists (
        select id from Employee a where   
        a.id = t.ManagerId 
        AND
        a.Salary < t.Salary 
    )

感觉都差不多...


找最高的

select max(Salary),ManagerId from Employee where ManagerId is not null

找全部的,如果索引包含ManagerId 可以在后面加上 ManagerId is not null缩小范围

select * from Employee where Salary>(select max(Salary) from Employee where ManagerId is null)

select name from Employee where ManagerId !=null and salary>(select min(salary) from Employee where ManagerId =null);
这样写应该也可以。 可能我理解你的问题理解错了,我觉得我的语句的结果,和你的应该不一样!

【热门文章】
【热门文章】