描述
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?
1 | +---------+---------+ |
For the sample input, the output is:
1 | +---------+---------+ |
Note:
If the number of students is odd, there is no need to change the last one’s seat.
分析
将表中的奇数行与偶数行交换位置,如果表的总行数是奇数,最后一行不变。
SQL 的 IF 函数类似于 A? B: C。
解决方案1(MySQL)
1 | # Write your MySQL query statement below |