leetcode刷mysql遇到的一些小tips

196

题目是删除重复邮箱,抄的解答

#因为mysql不允许同时对一个表delete和select联合操作select, 所以会select一个虚拟的表出来
delete
from Person where Id 
not in (select need.id from 
    (select min(Id) as id from Person group by Email) as need
);

以及作者说exist似乎可以优化一些, 有待探究

197

datedifftimestampdiff是后减前

select TIMESTAMPDIFF(DAY,'2019-05-20', '2019-05-21'); # 1

262

本题不熟悉的地方在于不知道 select后面的东西可以函数套函数

ELECT x.Request_at Day,ROUND(SUM(IF(Status!='completed',1,0))/COUNT(*),2) 'Cancellation Rate'
FROM (
    SELECT t.*
    FROM Trips  t, Users  u1, Users u2
    WHERE t.Client_Id=u1.Users_Id AND t.Driver_Id=u2.Users_Id
    AND u1.Banned='No' AND u2.Banned='No'
    AND Request_at BETWEEN '2013-10-01' AND '2013-10-03'
) x
GROUP BY x.Request_at
ORDER BY x.Request_at;

601

体育馆连续三天人流量超过100的date 不知道哪里有问题

# Write your MySQL query statement below
select a.* from stadium as a
inner join stadium as b
on a.id = b.id -1
inner join stadium as c
on b.id = c.id -1
where a.people >100
and b.people >100
and c.people >100
union
select a.* from stadium as a
inner join stadium as b
on a.id = b.id +1
inner join stadium as c
on b.id = c.id +1
where a.people >100
and b.people >100
and c.people >100;

评论里大神@Derrick 的思路:
我的猜测是在头查一次, 在尾查一次, 中间查一次
另一位大神@皮革马利翁 的对于去重操作解释:

比如, id为: 1, 2, 3, 4的满足, 我们讨论id 为2的这一天, id为2 满足是因为, 1, 2, 3; 也可以是因为2, 3, 4

每种情况都记录一次,那是不是这两种情况就记录了两次,重复了。

select distinct a.* from stadium a,stadium b,stadium c
where a.people>=100 and b.people>=100 and c.people>=100
and (
     (a.id = b.id-1 and b.id = c.id -1) or ##a<b<c
     (a.id = b.id-1 and a.id = c.id +1) or ##c<a<b
     (a.id = b.id+1 and b.id = c.id +1)    ##c<b<a
) order by a.id
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值