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 , 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值并且空值在后面,然后再按照mobile正序排序
时间: 2023-09-13 10:10:53 浏览: 72
好的,这是一个比较复杂的 SQL 语句,根据你的需求,我们可以进行如下修改:
```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 ELSE 1 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 语句会将 `tbl_scm_passenger` 表中的数据按照以下规则排序:
1. 如果 `id` 在指定的集合内,则排在前面,否则排在后面。
2. 如果 `phone` 为空值,则排在后面,否则排在前面。
3. 如果 `phone` 不为空,则按照 `phone` 的正序排列。
4. 如果 `id` 在指定的集合内,则按照 `mobile` 的正序排列。
希望这个答案能够解决你的问题。
阅读全文