上一篇:力扣MySQL刷题记录 点击阅读
本次更新的刷题记录如下
每道题,自己思路一个,吸取评论区思路一个,若和评论区答案差不多一样,则保留一个
--https://leetcode-cn.com/problems/exchange-seats/
--这个方法我不太懂
select b.id,a.student from seat as a,seat as b,(select count(*) as cnt from seat) as c where b.id=1^(a.id-1)+1 || (c.cnt%2 && b.id=c.cnt && a.id=c.cnt);
--懵逼方法2
select if(id%2=0,id-1,if(id=cnt,id,id+1)) as id,student from (select count(*) as cnt from seat)as a,seat order by id;
--懵逼方法3
select a.id,ifnull(b.student,a.student) as student from seat as a left join seat as b on (a.id%2=1 && a.id=b.id-1) || (a.id%2=0 && a.id=b.id+1) order by a.id;
--改ID的思路解法
select (case
when id%2=0 then id-1
when id=(select max(id) from seat) then id
else id+1 end) as id ,student
from seat order by id;
--双层if换ID
SELECT
IF(MOD(id, 2)= 0, id-1, IF(id=(
SELECT MAX(id) FROM seat
), id, id+1)) AS id, -- id为偶数则-1,为奇数且不是最大值则+1
student
FROM seat
ORDER BY id
--https://leetcode-cn.com/problems/customers-who-never-order/
--订单为0的客户
--官方解释
select name as Customers
from customers where customers.Id not in (select customerid from orders)
--https://leetcode-cn.com/problems/reformat-department-table/
--相对困难,两个表合并问题,且把1维度表改为了2维度表
--方法2
SELECT DISTINCT id AS "id",
SUM(IF (month = "Jan", revenue, null)) AS "Jan_Revenue",
SUM(IF (month = "Feb", revenue, null)) AS "Feb_Revenue",
SUM(IF (month = "Mar", revenue, null)) AS "Mar_Revenue",
SUM(IF (month = "Apr", revenue, null)) AS "Apr_Revenue",
SUM(IF (month = "May", revenue, null)) AS "May_Revenue",
SUM(IF (month = "Jun", revenue, null)) AS "Jun_Revenue",
SUM(IF (month = "Jul", revenue, null)) AS "Jul_Revenue",
SUM(IF (month = "Aug", revenue, null)) AS "Aug_Revenue",
SUM(IF (month = "Sep", revenue, null)) AS "Sep_Revenue",
SUM(IF (month = "Oct", revenue, null)) AS "Oct_Revenue",
SUM(IF (month = "Nov", revenue, null)) AS "Nov_Revenue",
SUM(IF (month = "Dec", revenue, null)) AS "Dec_Revenue"
FROM Department GROUP BY id
--https://leetcode-cn.com/problems/rank-scores/
--排序三种解说:https://leetcode-cn.com/problems/rank-scores/solution/tu-jie-sqlmian-shi-ti-jing-dian-pai-ming-wen-ti-by/
--一种思路
SELECT
a.Score AS Score,
COUNT(DISTINCT b.Score) AS Rank -- 注意按题目要求去重复值
FROM Scores AS a, Scores AS b
WHERE b.Score >= a.Score -- 表b中有x个非重复值大于等于表a当前值,则表a当前成绩排名为x
GROUP BY a.id -- 由于成绩即使重复也要显示,故通过id分组
ORDER BY a.Score DESC
--https://leetcode-cn.com/problems/department-highest-salary/
--我自己只能写到这个层次目前,但并没有解决问题
select d.name as Department , e.name as Employee, e.salary
from employee as e left join department as d
on e.departmentId = d.Id
group by d.name
--官方解释 表子查询 + 内连接
SELECT
Department.name AS 'Department',
Employee.name AS 'Employee',
Salary
FROM
Employee
JOIN
Department ON Employee.DepartmentId = Department.Id
WHERE
(Employee.DepartmentId , Salary) IN
( SELECT
DepartmentId, MAX(Salary)
FROM
Employee
GROUP BY DepartmentId
)
;
--https://leetcode-cn.com/problems/department-top-three-salaries/
--参考答案里有效代码如下
SELECT
Department.NAME AS Department,
e1.NAME AS Employee,
e1.Salary AS Salary
FROM
Employee AS e1,Department
WHERE
e1.DepartmentId = Department.Id
AND 3 > (SELECT count( DISTINCT e2.Salary )
FROM Employee AS e2
WHERE e1.Salary < e2.Salary AND e1.DepartmentId = e2.DepartmentId )
ORDER BY Department.NAME,Salary DESC;
--三个连续日期大于100人流量 https://leetcode-cn.com/problems/human-traffic-of-stadium/
--一个解析所云:https://leetcode-cn.com/problems/human-traffic-of-stadium/solution/ti-yu-guan-de-ren-liu-liang-by-little_bird/
SELECT distinct a.*
FROM stadium as a,stadium as b,stadium as c
where ((a.id = b.id-1 and b.id+1 = c.id) or
(a.id-1 = b.id and a.id+1 = c.id) or
(a.id-1 = c.id and c.id-1 = b.id))
and (a.people>=100 and b.people>=100 and c.people>=100)
order by a.id;
--另一种解法:
select
id, to_char(visit_date, 'yyyy-mm-dd') as visit_date, people
from
(select
id, visit_date, people,
count(1) over (partition by offset) cnt
from
(select
id, visit_date, people,
(row_number() over (order by id) - id) offset
from stadium
where people >= 100
)
)
where cnt >= 3 -- 连续 3 天(及以上)
order by id
--第三种解法
SELECT DISTINCT S1.*
FROM stadium AS S1,stadium AS S2,stadium AS S3
WHERE S1.people>=100 AND S2.people>=100 AND S3.people>=100 AND (
S1.id +1 = S2.id AND S1.id+2=S3.id OR
S1.id +1 = S2.id AND S1.id-1=S3.id OR
S1.id -1 = S2.id AND S1.id-2=S3.id
)
ORDER BY S1.id
--
--解法1
SELECT T.request_at AS `Day`,
ROUND(
SUM(
IF(T.STATUS = 'completed',0,1)
)
/
COUNT(T.STATUS),
2
) AS `Cancellation Rate`
FROM Trips AS T
JOIN Users AS U1 ON (T.client_id = U1.users_id AND U1.banned ='No')
JOIN Users AS U2 ON (T.driver_id = U2.users_id AND U2.banned ='No')
WHERE T.request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY T.request_at
--解法2
SELECT t.Request_at AS `Day`,ROUND(SUM(CASE t.`Status` WHEN 'cancelled_by_client' THEN 1 WHEN 'cancelled_by_driver' THEN 1 ELSE 0 END)/COUNT(t.`Status`) ,2)AS `Cancellation Rate`
FROM Trips t
JOIN Users d ON t.Client_Id = d.Users_Id AND d.Banned = 'NO'
JOIN Users c ON t.Driver_Id = c.Users_Id AND c.Banned = 'NO'
WHERE t.Request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY t.Request_at