SELECT * FROM emm_task.tbl_scm_passenger ORDER BY CASE WHEN id IN (53213970,53213971,53213972,53213973,53213974,53213975,53213976,53213977,53213978,53213979,53213980,53213981,53213982,53213983,53213984,53213985,53213986,53213987,53213988,53213989) then 0 ELSE 1 END, CASE WHEN phone IS NULL THEN 1 WHEN id IN (53213970,53213971,53213972,53213973,53213974,53213975,53213976,53213977,53213978,53213979,53213980,53213982,53213983,53213984,53213985,53213986,53213987,53213988,53213989) THEN 0 ELSE 2 END, phone ASC, CASE WHEN id IN (53213970,53213971,53213972,53213973,53213974,53213975,53213976,53213977,53213978,53213979,53213980,53213982,53213983,53213984,53213985,53213986,53213987,53213988,53213989) THEN mobile end asc 修改这段sql让关于phone的排序空值在后面
时间: 2023-08-01 21:10:54 浏览: 52
以下是修改后的 SQL 语句:
```
SELECT *
FROM emm_task.tbl_scm_passenger
ORDER BY
CASE WHEN id IN (53213970,53213971,53213972,53213973,53213974,53213975,53213976,53213977,53213978,53213979,53213980,53213981,53213982,53213983,53213984,53213985,53213986,53213987,53213988,53213989) THEN 0 ELSE 1 END,
CASE WHEN phone IS NULL THEN 2 WHEN id IN (53213970,53213971,53213972,53213973,53213974,53213975,53213976,53213977,53213978,53213979,53213980,53213982,53213983,53213984,53213985,53213986,53213987,53213988,53213989) THEN 0 ELSE 1 END,
phone ASC NULLS LAST,
CASE WHEN id IN (53213970,53213971,53213972,53213973,53213974,53213975,53213976,53213977,53213978,53213979,53213980,53213982,53213983,53213984,53213985,53213986,53213987,53213988,53213989) THEN mobile ELSE NULL END ASC NULLS LAST
```
修改步骤如下:
1. 将第二个 `CASE` 表达式中 `phone IS NULL` 的排序值由 1 修改为 2,这样 `phone` 为空的记录就会在后面排列。
2. 在 `phone ASC` 后添加 `NULLS LAST`,确保 `phone` 为空的记录在排序时在后面。
3. 在最后一个 `CASE` 表达式中,当 `id` 在指定的范围内时,才按照 `mobile` 排序,否则返回 `NULL`。并添加 `NULLS LAST` 确保 `mobile` 为空的记录在排序时在后面。
阅读全文