leetcode603. Continuous spare seat (SQL)

Several friends came to the movie theater ticket office, ready to make an appointment consecutive spare seats.

You can use the table cinema, help them write a query to get all the vacant seats, and they returned in the seat_id sort it?

| seat_id | free |
|---------|------|
| 1       | 1    |
| 2       | 0    |
| 3       | 1    |
| 4       | 1    |
| 5       | 1    |
 

For the above examples, your query should return the following results.

 

| SEAT_ID |
| --------- |
| 3 |
| 4 |
| 5 |
Note:

seat_id field is an integer from growing, free field is a Boolean type ( '1' vacant, '0' is occupied).
Define a continuous seat availability is greater than or equal to 2 consecutive vacant seat.

Ideas: a self-join condition is adjacent and free.

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

 

Published 584 original articles · won praise 10000 + · views 1.39 million +

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104390997