美文网首页
LeetCode 626. Exchange Seats

LeetCode 626. Exchange Seats

作者: 卡尔是正太 | 来源:发表于2017-12-17 20:19 被阅读0次

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.

题目大意

玛丽编写一个查询以改变邻近学生的座位,如果学生人数是奇数,则不需要改变最后一个座位。

解题思路

与其变换名称,不如将id为2n变换为2n-1,2n-1变换为2n (n=1,2,3....)
最好讲结果分成两部分,一部分处理奇数id的变换 一部分处理偶数id的变化
若不考虑边界情况,则易得sql如下

SELECT tmp.id, tmp.student FROM
(
    SELECT id-1 AS id, student FROM seat WHERE id%2 = 0  -- 偶数 id -1
    UNION
    SELECT id+1 AS id, student FROM seat WHERE id%2 = 1  -- 奇数 id +1
    
) tmp
ORDER BY tmp.id 

考虑边界则要在update中去除一个id,这个id必须满足是奇数并且是最大的id

则得sql

SELECT tmp.id, tmp.student FROM
(
    SELECT id-1 AS id, student FROM seat WHERE id%2 = 0  -- 偶数 id -1
    UNION
    SELECT CASE 
        WHEN id = max_id  AND max_id%2 = 1 THEN id 
        ELSE id + 1 END -- 使用END 结束case
            AS id, 
    student 
    FROM seat,(SELECT max(id) as max_id FROM seat) AS t_max_id -- 这里实际上seat表和一个只有一个元组的seat最大临时表做了笛卡儿积 
    WHERE id%2 = 1  -- 奇数 id +1
) tmp
ORDER BY tmp.id 

这里值得注意的是,若你是使用end case 结束case则会报错


END case 报错

而实际上 MySQL 5.7 规定的标准格式为CASE...END CASE


详见MySQL 5.7 Reference Manual - 13.6.5.1 CASE Syntax

然后特意找了一台5.7的数据库服务器上重新运行了一下,结果竟然还是报错了


并且发在网上查阅资料的时候只见到了case... end 的写法 没有case... end case

难道是只在begin ... end 内才这么写 case end 用于区分 不同的结束标志吗?
详细原因还需要再继续学习,若你有正确的认识或想法也请留言或联系我

相关文章

  • LeetCode 626. Exchange Seats

    LeetCode 626. Exchange Seats 题目 Mary is a teacher in a mi...

  • 626. Exchange Seats

    Mary is a teacher in a middle school and she has a table ...

  • 626. Exchange Seats

    题目链接: 626. Exchange Seats 解析: 本题是一个交换的问题,比较复杂,首先根据题目要求可以知...

  • SQL in leetcode——medium篇

    https://leetcode.com/problems/exchange-seats/ 交换相邻学生的id,很...

  • leetcode题

    Exchange Seats 玛丽是一所中学的老师,她又一个表为 seat ,上面有学生的名字和相应的座位号。列i...

  • 【LeetCode】626. 换座位

    LeetCode数据库题目 题目 小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和...

  • 02 在飞机上提出请求

    笔记: 换座位 switch seats=swap seats 笔记: 注意aisle的发音 笔记: lift t...

  • 旅行英语用法记录

    window seats 靠窗的座位 aisle seats 靠过道的座位 can i trade my seat...

  • courtesy seats

    今天坐地铁看到一排黄色的很有爱的爱心专座,上面用英文标注着courtesy seats, 想到以前自己一直记不住这...

  • MySQL 函数 | GROUP_CONCAT

    把结果集中 seats_ids 列的值连起来 SELECT GROUP_CONCAT(seats_ids) FRO...

网友评论

      本文标题:LeetCode 626. Exchange Seats

      本文链接:https://www.haomeiwen.com/subject/wwruwxtx.html