LeetCode-603. 连续空余座位(简单)

LeetCode-603. 连续空余座位(简单)_第1张图片
LeetCode-603. 连续空余座位(简单)_第2张图片
题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/consecutive-available-seats
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

本人思路:
– 1、连续的空余座位,首先想到表的自联结,注意连接的条件,abs(c1.seat_id - c2.seat_id)=1,即seat_id相差±1的都可以,并且c1.free =‘1’ and c2.free=‘1’,不可以写成c1.free =c2.free=‘1’(这个细节当时找半天才找到)

select distinct c1.seat_id
from cinema as c1 ,cinema as c2 
where abs(c1.seat_id - c2.seat_id)=1 and c1.free ='1' and c2.free='1'
order by c1.seat_id

你可能感兴趣的:(数据库刷题)