leetcode_mysql_day2

1. 超过经理收入的员工

在这里插入图片描述
解答1:

 select a.Name Employee from Employee a where  a.ManagerId is not null AND a.Salary >(
 select b.Salary from Employee b where a.ManagerId =b.Id
 ) 
  1. 使用is not null的方式 去除是经理没有上司的员工
  2. 通过子查询 查找员工工资高于经理的员工:
  3. 缺点效率很低 但是好思考

解答2:通过inner join...on..的方式来进行查询 效率很高

# 使用inner join ...on 会把a.managerid = b.id 相同的=的字段相同的进行查询出来
select a.name as Employee
from employee a inner join employee b on a.managerid = b.id 
and a.salary > b.salary

推荐学习连接,嘻嘻保存:mysql内连接 左连接 右连接

2. 找出重复的子邮箱

在这里插入图片描述
题解1:

  • 合理的使用聚合函数count 与group by 的结合
  • 利用having对查询出的视图再进行添加条件
SELECT Email FROM Person GROUP BY Email HAVING COUNT(Email)>1

推荐博客—
where group by order by的使用顺序
题解2:

  • 通过distinct来排除自己 并且通过id不相等来找寻重复次数>2的
select distinct a.Email
from Person a,Person b
where a.Email=b.Email
and a.Id<>b.Id;

3. 从不订购客户

在这里插入图片描述
题解1:

select a.Name as 'Customers'  from Customers as a 
	where a.id not in 
		(select CustomerId from Orders ) 

题解2:

  • 通过左连接 找出 以左cumstomers为主的数据
  • 通过 is null 得到没有订单的用户name
select A.Name as Customers
from Customers A left join Orders B
on A.Id = B.CustomerId
where B.Id is null

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值