leetcode-sql-刷题汇总(补充ing)

leetcode-sql-刷题汇总

607. 销售员

https://leetcode-cn.com/problems/sales-person/

# 思路1: not in 
select name 
from 
SalesPerson
where name not in 
(select s.name
from 
Company as c
join 
Orders as o 
on c.com_id = o.com_id
join 
SalesPerson as s 
on o.sales_id = s.sales_id
where c.name = 'RED')


# 思路2: 不需要找到人名,只需要找到id,排除id即可
select
    name
from
    salesperson
where
    sales_id not in 
    (select sales_id from orders where com_id in 
        (select com_id from company where name = 'RED'))

197. 上升的温度

https://leetcode-cn.com/problems/rising-temperature/

# Write your MySQL query statement below

# 这个不对,因为数据可能不是日期连续的。。。
select id 
from 
(select 
id, 
lag(Temperature, 1) over (order by recordDate) as preTemperature, Temperature,
lag(recordDate, 1) over (order by recordDate) as preRecordDate, recordDate
from
Weather)as t 
where 1 
and DATE_ADD(preRecordDate, interval 1 day) = recordDate
and Temperature > preTemperature 




# 思路2:就是笛卡尔积,然后判断出来即可
select t2.id 
from 
Weather as t1 
join 
Weather as t2 
where 1
and DATE_ADD(t1.recordDate, interval 1 day) = t2.recordDate
and t2.Temperature > t1.Temperature

1148. 文章浏览 I

https://leetcode-cn.com/problems/article-views-i/

# 思路1: 就where即可
select distinct author_id as id
from Views 
where author_id = viewer_id
order by author_id


1581. 进店却未进行过交易的顾客

https://leetcode-cn.com/problems/customer-who-visited-but-did-not-make-any-transactions/

# 思路1: left join 
select v.customer_id, 
count(1) as count_no_trans
from 
Visits as v 
left join 
Transactions as t 
on v.visit_id = t.visit_id
where t.transaction_id is null
group by v.customer_id

608. 树节点

https://leetcode-cn.com/problems/tree-node/

# 思路1: 就关联出父节点和子节点即可
select id, 
if(p_count > 0 && c_count > 0, 'Inner', if(p_count = 0, 'Root', 'Leaf')) as type
from 
(select t1.id, 
# t2.id as p_id, t3.id as child_id,
count(t2.id) as p_count,
count(t3.id) as c_count
from 
tree as t1 
left join 
tree as t2 
on t1.p_id = t2.id
left join 
tree as t3 
on t3.p_id = t1.id
group by id) <
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值