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. nohup 同时实现记录日志和屏幕输出

    nohup   nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户/关闭终端之后继续运行相应的进程.nohup就是不挂断 ...

  2. F#周报2018年第52期

    新闻 Sudokube--使用Fable开发的数独立方体 Rust 2019年及以后的发展 视频及幻灯片 我爱F#代码 马蒂亚斯·布兰在Developer On Fire上的演讲--有条理的和有趣的 ...

  3. nowcoder 206A - Birthday - [最小费用最大流]

    题目链接:https://www.nowcoder.com/acm/contest/206/A 题目描述 恬恬的生日临近了.宇扬给她准备了一个蛋糕.正如往常一样,宇扬在蛋糕上插了n支蜡烛,并把蛋糕分为 ...

  4. 【鬼畜】UVA - 401每日一题·猛男就是要暴力打表

    管他什么rev数组,msg数组简化代码 #define _CRT_SECURE_NO_WARNINGS #include <cmath> #include <iostream> ...

  5. deepin中idea中文乱码解决

    打开终端 命令: sudo su 输入密码: aptitude search uming 显示: root@terwer-PC:/home/terwer# aptitude search uming ...

  6. Chap2:区块链基本技术[《区块链中文词典》维京&甲子]

  7. [development][security][modsecurity][nginx] nginx / modsecurity development things

    接续前节:[security][modsecurity][nginx] nginx 与 modsecurity nginx开发手册:https://nginx.org/en/docs/dev/deve ...

  8. Celery的Web监控管理--Flower

    Flower是Celery的一个实时监控和管理Web界面工具,目前仍在活跃的开发之中,但已经是一个很重要的可用工具了.这是推荐使用的Celery监控工具. 1,安装依赖 pip install flo ...

  9. NumPy 广播机制(Broadcasting)

    一.何为广播机制 a.广播机制是Numpy(开源数值计算工具,用于处理大型矩阵)里一种向量化数组操作方法. b.Numpy的通用函数(Universal functions) 中要求输入的两个数组sh ...

  10. HBase系列文章(转)

    HBase概念学习(一)基本架构 HBase概念学习(二)JAVA API操作概览 HBase概念学习(三)Java API之CRUD(增查改删) HBase概念学习(四)Java API之扫描和过滤 ...