leetcode上的数据库题汇总(4)

  1. Human Traffic of Stadium

https://leetcode.com/problems/human-traffic-of-stadium/

select s.id,s.date,s.people from stadium s where people >= 100 and
(
    (
        (select people from stadium where id = s.id + 1) >= 100 
        and 
        (select people from stadium where id = s.id - 1) >= 100
    )
    or
    (
        (select people from stadium where id = s.id - 1) >= 100 
        and 
        (select people from stadium where id = s.id - 2) >= 100
    ) 
    or
    (
        (select people from stadium where id = s.id + 1) >= 100 
        and 
        (select people from stadium where id = s.id + 2) >= 100
    )
);
 
select * from stadium s where s.people >= 100 and 
(
(
s.id+1 in (select id from stadium where people >= 100) 
and 
s.id+2 in (select id from stadium where people >= 100) 
)
or 
(
s.id-1 in (select id from stadium where people >= 100) 
and 
s.id+1 in (select id from stadium where people >= 100) 
)
or 
(
s.id-1 in (select id from stadium where people >= 100) 
and 
s.id-2 in (select id from stadium where people >= 100) 
)
);
 
  1. Not Boring Movies

https://leetcode.com/problems/not-boring-movies/

# Write your MySQL query statement below
select *
from cinema
where mod(id,2) = 1 and description <> 'boring'
order by rating desc
  1. Exchange Seats

https://leetcode.com/problems/exchange-seats/

select case when id = (select max(id) from seat) and mod(id, 2) = 1 then id
            when id < (select max(id) from seat) and mod(id, 2) = 1 then id + 1
            when mod(id, 2) = 0 then id - 1
            end as id,student
from seat
order by id
  1. Swap Salary

https://leetcode.com/problems/swap-salary/

Update salary 
SET sex = CASE sex WHEN 'f' THEN 'm' ELSE 'f' END;
update salary set sex=IF(sex='m','f', 'm')
UPDATE salary 
SET sex = CHAR(ASCII('f') ^ ASCII('m') ^ ASCII(sex))

UPDATE salary
SET sex = CHAR(ASCII(sex) ^ 11)

你可能感兴趣的:(笔试题面试题刷题,LeetCode刷题练习,mysql数据库总结,leetcode做题代码合集)