Several friends at a cinema ticket office would like to reserve consecutive available seats.
Can you help to query all the consecutive available seats order by the seat_id using the following cinema table?

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

Your query should return the following result for the sample case above.

| seat_id |
|---------|
| 3 |
| 4 |
| 5 |

Note:

  • The seat_id is an auto increment int, and free is bool ('1' means free, and '0' means occupied.).
  • Consecutive available seats are more than 2(inclusive) seats consecutively available.

这个题目参考solution, 就是先得到seat_id 和所有seat_id对应的表格, 然后再选两个的差值是1的, 最后Distinct 并且 in order.

Code

SELECT DISTINCT c1.seat_id FROM cinema AS c1 JOIN cinema AS c2 WHERE abs(c1.seat_id - c2.seat_id) = 1 and c1.free = True and c2.free = True
order by c1.seat_id

[LeetCode] 603. Consecutive Available Seats_Easy tag: SQL的更多相关文章

  1. [LeetCode] 619. Biggest Single Number_Easy tag: SQL

    Table number contains many numbers in column num including duplicated ones.Can you write a SQL query ...

  2. [LeetCode] 620. Not Boring Movies_Easy tag: SQL

    X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a ...

  3. [LeetCode] 176. Second Highest Salary_Easy tag: SQL

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

  4. [LeetCode] 196. Delete Duplicate Emails_Easy tag: SQL

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  5. [LeetCode] 584. Find Customer Referee_Easy tag: SQL

    Given a table customer holding customers information and the referee. +------+------+-----------+ | ...

  6. LeetCode Database: Consecutive Numbers

    Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...

  7. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

  9. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

随机推荐

  1. JVM内存GC的骗局——JVM不抛出OOM但内存已经泄露

    概述 在日常测试中,我们会去重点观察java的内存使用情况,比如:进程会抛出OOM异常,不再接收新的请求:响应时间在固定时间段内变长,超时或者不响应,CPU使用率时常像过山车一样等.有时候JVM还会发 ...

  2. 中小学教育缴费----支付宝回传数据.net core 接收中文乱码

    问题描述: 中小学教育缴费,发送账单到家长支付宝,家长支付成功之后,支付宝回传数据,验签的时候失败了,排查之后发现账单名称乱码了.支付宝回传的时候中文传的是GBK编码格式,但是我接收的是%D5˵%A5 ...

  3. 词云绘制wordcloud

    wordcloud是优秀的第三方词云展示库,该库以空格为分割线,按照单词出现的频率自动设置字号与颜色实例如下 import wordcloud#词云库 import jieba#分词库 a=open( ...

  4. VMWARE虚拟机不显示主机共享的文件夹解决办法

    执行如下命令重新配置,不用重启. #sudo vmware-config-tools.pl

  5. .NET Core开发日志——配置

    熟悉ASP.NET的开发者一定对web.config文件不陌生.在ASP.NET环境中,要想添加配置参数,一般也都会在此文件中操作.其中最常用的莫过于AppSettings与ConnectionStr ...

  6. Warning: Failed to halt at after bootloader, forced stop at

    该错误证实是因为 cc2650 SW下载模式,芯片复位引脚未接出来导致,芯片复位必须和下载器保持良好连接

  7. DRBD数据镜像与搭建

    一.数据安全工具DRDB 1. 数据镜像软件DRDB介绍 分布式块设备复制,是基于软件.基于网络的块复制存储解决方案 作用:用于服务器之间的磁盘.分区.逻辑卷等进行数据镜像. 例如:当用户将数据写入本 ...

  8. php新加扩展模块

    记录下在已经编译安装的PHP上面增加扩展模块,下面以安装mbstring.so为例 1.进入PHP源码文件中的mbstring文件夹,一般都是在ext目录 cd php-5.5.29/ext/mbst ...

  9. 结构体地址 字符串地址 数组地址 辨析 字符char是整型 内存地址

    小结: 1.函数传参中,结构体不同数组,结构体是传值,指针和数组是传地址:2.随声明顺序,指针变量的内存地址从低到高,其他从高到低:3.char c[]字符数组,即数组的一种:char *c字符指针, ...

  10. [skill] 补码

    转载,写的很好!额,我的数学. 原文:https://www.douban.com/note/223507364/ 关于补码,看过一些书籍和网文,基本都是在“求反加一”的方法.步骤上反复强调,而对于补 ...