[Swift]LeetCode626. 换座位 | Exchange Seats
SQL架构 1 Create table If Not Exists seat(id int,student varchar(255)) 2 Truncate table seat 3 insert into seat (id,student) values (‘1‘,‘Abbot‘) 4 insert into seat (id,student) values (‘2‘,‘Doris‘) 5 insert into seat (id,student) values (‘3‘,‘Emerson‘) 6 insert into seat (id,student) values (‘4‘,‘Green‘) 7 insert into seat (id,student) values (‘5‘,‘Jeames‘) Mary is a teacher in a middle school and she has a table? 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: 小美是一所中学的信息科技老师,她有一张? 其中纵列的?id?是连续递增的 小美想改变相邻俩学生的座位。 你能不能帮她写一个 SQL query?来输出小美想要的结果呢?? 示例: +---------+---------+ | id | student | +---------+---------+ | 1 | Abbot | | 2 | Doris | | 3 | Emerson | | 4 | Green | | 5 | Jeames | +---------+---------+ 假如数据输入的是上表,则输出结果如下: +---------+---------+ | id | student | +---------+---------+ | 1 | Doris | | 2 | Abbot | | 3 | Green | | 4 | Emerson | | 5 | Jeames | +---------+---------+ 注意: 如果学生人数是奇数,则不需要改变最后一个同学的座位。 Runtime:?152 ms 1 # Write your MySQL query statement below 2 select 3 (case when id % 2 = 0 then id - 1 4 when id % 2 = 1 and id <> c.cnt then id + 1 5 else id 6 end) as id,student 7 from 8 seat, 9 (select count(id) as cnt from seat) as c 10 order by id 253ms 1 # Write your MySQL query statement below 2 SELECT 3 (CASE 4 WHEN MOD(id,2) != 0 AND counts != id THEN id + 1 5 WHEN MOD(id,2) != 0 AND counts = id THEN id 6 ELSE id - 1 7 END) as id, 8 student 9 FROM 10 seat,11 (SELECT COUNT(*) as counts 12 FROM seat) as seat_counts 13 ORDER BY id ASC; 255ms 1 # Write your MySQL query statement below 2 select * from ( 3 select case when b.Id is null then a.Id else b.Id end as Id,a.student from seat a left join seat b on a.Id+1=b.Id where mod(a.id,2)=1 4 union 5 select b.Id,a.student from seat a left join seat b on a.Id-1=b.Id where mod(a.id,2)=0 6 ) a order by a.id 257ms 1 select 2 case 3 when id%2=1 and id=(select max(id) from seat) then id 4 when id%2=1 then id+1 5 else id-1 end as id,6 student 7 FROM seat 8 order by id 258ms 1 # Write your MySQL query statement below 2 select case 3 when mod(id,2) =1 and id = (select max(id) as sid from seat) then id 4 when mod(id,2) =1 then id+1 5 else id-1 end as id,student 6 from seat 7 order by id 260ms 1 SELECT 2 (CASE WHEN id%2=0 THEN id-1 3 WHEN id%2=1 AND id<(select max(id) FROM seat) THEN id+1 4 ELSE id 5 END) as id 6 ,student 7 FROM seat 8 ORDER BY id; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 解决:Flash Buider 4.6与Chrome调试console出不
- swift – 如何修复错误“无法使用类型的参数列表
- ruby-on-rails – rails中的属性和构造函数
- 相当于Swing的包在C#
- ruby-on-rails – twitter-bootstrap-rails gem工
- ruby-on-rails – 带门卫的Rails中的自定义401页
- postgresql – plgpsql – 是否可以声明setof记录
- ios – Receiver没有标识符execption的segue
- C++中类中范围解析运算符::和点运算符(.)
- qt 制作记事本:自动备份,(正则表达式)搜索替