LeetCode-SQL-1204. 最后一个能进入电梯的人

LeetCode-SQL-1204. 最后一个能进入电梯的人_第1张图片

LeetCode-SQL-1204. 最后一个能进入电梯的人_第2张图片

Create table If Not Exists Queue (person_id int, person_name varchar(30), weight int, turn int)
Truncate table Queue
insert into Queue (person_id, person_name, weight, turn) values ('5', 'George Washington', '250', '1')
insert into Queue (person_id, person_name, weight, turn) values ('4', 'Thomas Jefferson', '175', '5')
insert into Queue (person_id, person_name, weight, turn) values ('3', 'John Adams', '350', '2')
insert into Queue (person_id, person_name, weight, turn) values ('6', 'Thomas Jefferson', '400', '3')
insert into Queue (person_id, person_name, weight, turn) values ('1', 'James Elephant', '500', '6')
insert into Queue (person_id, person_name, weight, turn) values ('2', 'Will Johnliams', '200', '4')
select 
    person_name        
from (
    select
        person_name,      
        @num:=@num+(@pre:=weight) as num    
    from Queue q,(select @num:=0,@pre:=0)s order by turn asc
)s1 where num<=1000 order by num desc limit 1

作者:stop-si-nian
链接:https://leetcode-cn.com/problems/last-person-to-fit-in-the-elevator/solution/mysql-zi-ding-yi-bian-liang-by-stop-si-nian/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
select person_name
from Queue q1
where (select sum(weight) from Queue where turn <= q1.turn) <= 1000
order by turn desc limit 1

作者:couchpotato613
链接:https://leetcode-cn.com/problems/last-person-to-fit-in-the-elevator/solution/mysqlshi-xian-by-couchpotato613/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

你可能感兴趣的:(数据库,LeetCode)