LeetCode - 626. Exchange Seats
Mary is a teacher in a middle school and she has a table seat
storing students' names and their corresponding seat ids.
The column id is continuous increment.
Mary wants to change seats for the adjacent students.
Can you write a SQL query to output the result for Mary?
- +---------+---------+
- | id | student |
- +---------+---------+
- | 1 | Abbot |
- | 2 | Doris |
- | 3 | Emerson |
- | 4 | Green |
- | 5 | Jeames |
- +---------+---------+
For the sample input, the output is:
- +---------+---------+
- | id | student |
- +---------+---------+
- | 1 | Doris |
- | 2 | Abbot |
- | 3 | Green |
- | 4 | Emerson |
- | 5 | Jeames |
- +---------+---------+
Note:
If the number of students is odd, there is no need to change the last one's seat.
- # Write your MySQL query statement below
- SELECT
- s.id,
- s.student
- FROM
- (
- SELECT
- id - 1 AS id,
- student
- FROM
- seat
- WHERE
- (id % 2 = 0)
- UNION
- SELECT
- (CASE WHEN (cnt%2=1) AND id=cnt THEN id ELSE id + 1 END) AS id,
- student
- FROM
- seat,
- (select count(*) as cnt from seat) as seatcnt
- WHERE
- (id % 2 = 1)
- ) s
- GROUP BY
- s.id ASC
LeetCode - 626. Exchange Seats的更多相关文章
- 【leetcode】Exchange Seats
Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...
- [SQL]LeetCode626. 换座位 | Exchange Seats
SQL架构 Create table If Not Exists seat(id )) Truncate table seat insert into seat (id, student) value ...
- 626. Exchange Seats-(LeetCode之Database篇)
问题表述 数据库表如下: id student 1 Abbot 2 Doris 3 Emerson 4 Green 5 Jeames 现在要通过SQL语句将表变换成如下: id student 1 D ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Leetcode中的SQL题目练习(二)
175. Combine Two Tables https://leetcode.com/problems/combine-two-tables/description/ Description Pe ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【sql】leetcode习题 (共 42 题)
[175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...
- LeetCode:626.换座位
题目链接:https://leetcode-cn.com/problems/exchange-seats/ 题目 小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们 ...
- 【LeetCode题解】排序
1. 排序 排序(sort)是一种常见的算法,把数据根据特定的顺序进行排列.经典的排序算法如下: 冒泡排序(bubble sort) 插入排序(insertion sort) 选择排序(selecti ...
随机推荐
- Ubuntu16.04安装mongodb
Ubuntu16.04安装mongodb copy from: http://blog.csdn.net/zhushh/article/details/52451441 1.导入软件源的公钥 sudo ...
- JavaScript获取当前url根目录(路径)
jsp: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&q ...
- pjax 笔记
PJAX的基本思路是,用户点击一个链接,通过ajax更新页面变化的部分,然后使用HTML5的pushState修改浏览器的URL地址,这样有效地避免了整个页面的重新加载.如果浏览器不支持history ...
- Performance Testing 入门小结
从事软件测试两年多了,一直在做功能测试.2016年计划学习Performance.今天,先把之前听过的同事session以及自己查阅的资料小结一下. 一.什么是性能测试 首先来说一下软件的性能是什么. ...
- 全新的软件项目,好的开始决定了成功一半!(需求&计划)
刚看完“无问西东”,电影里说人总归还是要留下些足迹(文字)的,那么赶紧跑图书馆来留下些文字. 最近去瑞士启动了一个新的项目,那么早上做项目,晚上总结留下了一张张思维导图来记录当时的感受, 手稿如下,字 ...
- linux 巨页使用测试以及勘误1
linux使用hugetlbfs的方式来支持巨页,也成为大页. 网上看到有人说巨页不支持read,和write调用,只支持mmap,但是看3.10内核代码的时候发现: const struct fil ...
- 【问题处理】mysql sleep 连接数过多
睡眠连接过多,会对mysql服务器造成什么影响?严重消耗mysql服务器资源(主要是cpu, 内存),并可能导致mysql崩溃.造成睡眠连接过多的原因?1. 使用了太多持久连接(个人觉得,在高并发系统 ...
- Hystrix-request cache(请求缓存)
开启请求缓存 请求缓存在run()和construce()执行之前生效,所以可以有效减少不必要的线程开销.你可以通过实现getCachekey()方法来开启请求缓存. package org.hope ...
- python_18_反射
什么是反射? -- 通过输入字符串来获取和修改 类(属性+方法),用字符串来映射内存对象,用于人机交互 反射有哪几种方法? -- getattr() --获取字符串 ...
- 移动端 -webkit-user-select:text; ios10 bug 解决方案
移动端一般body的css.会设置 作用就不解释了: body{ height:100%;min-height:100%; font-family: "微软雅黑",'Helveti ...