Skip to content

Commit d404e6a

Browse files
committed
feat: add sql solution to lc problems: No.0626
No.0626.Exchange Seats
1 parent c7de30e commit d404e6a

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

solution/0600-0699/0626.Exchange Seats/README.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,31 @@ Seat 表:
6565

6666
### **SQL**
6767

68-
```
68+
```sql
6969
SELECT
70-
s1.id, COALESCE(s2.student, s1.student) AS student
70+
s1.id,
71+
COALESCE ( s2.student, s1.student ) AS student
7172
FROM
72-
seat s1
73-
LEFT JOIN
74-
seat s2 ON (s1.id+1)^1-1 = s2.id
75-
ORDER BY s1.id;
73+
seat s1
74+
LEFT JOIN seat s2 ON ( s1.id + 1 ) ^ 1 - 1 = s2.id
75+
ORDER BY
76+
s1.id;
77+
```
78+
79+
```sql
80+
SELECT
81+
id + (
82+
CASE
83+
WHEN id % 2 = 1 AND id != (SELECT MAX(id) FROM seat) THEN 1
84+
WHEN id % 2 = 0 THEN -1
85+
ELSE 0
86+
END
87+
) AS id,
88+
student
89+
FROM
90+
seat
91+
ORDER BY
92+
id;
7693
```
7794

7895
<!-- tabs:end -->

solution/0600-0699/0626.Exchange Seats/README_EN.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,31 @@ Note that if the number of students is odd, there is no need to change the last
6161

6262
### **SQL**
6363

64-
```
64+
```sql
6565
SELECT
66-
s1.id, COALESCE(s2.student, s1.student) AS student
66+
s1.id,
67+
COALESCE ( s2.student, s1.student ) AS student
6768
FROM
68-
seat s1
69-
LEFT JOIN
70-
seat s2 ON (s1.id+1)^1-1 = s2.id
71-
ORDER BY s1.id;
69+
seat s1
70+
LEFT JOIN seat s2 ON ( s1.id + 1 ) ^ 1 - 1 = s2.id
71+
ORDER BY
72+
s1.id;
73+
```
74+
75+
```sql
76+
SELECT
77+
id + (
78+
CASE
79+
WHEN id % 2 = 1 AND id != (SELECT MAX(id) FROM seat) THEN 1
80+
WHEN id % 2 = 0 THEN -1
81+
ELSE 0
82+
END
83+
) AS id,
84+
student
85+
FROM
86+
seat
87+
ORDER BY
88+
id;
7289
```
7390

7491
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
SELECT
2-
s1.id, COALESCE(s2.student, s1.student) AS student
2+
s1.id,
3+
COALESCE ( s2.student, s1.student ) AS student
34
FROM
4-
seat s1
5-
LEFT JOIN
6-
seat s2 ON (s1.id+1)^1-1 = s2.id
7-
ORDER BY s1.id;
5+
seat s1
6+
LEFT JOIN seat s2 ON ( s1.id + 1 ) ^ 1 - 1 = s2.id
7+
ORDER BY
8+
s1.id;

0 commit comments

Comments
 (0)