在以下情况下我需要你的帮助:
我有一张看起来像这样的表
----------------------------
| id | order | highlighted |
----------------------------
| 1 | 5 | 1 |
----------------------------
| 2 | 7 | 1 |
----------------------------
| 3 | 0 | 0 |
----------------------------
| 4 | 0 | 0 |
----------------------------
| 5 | 9 | 1 |
----------------------------
| *and so on* |
----------------------------
id
是INT(11)AUTO_INCREMENT order
是INT(11)highlighted
是INT(11),仅包含'1'或'0'值我想在一个查询中对这些条目进行排序,如下所示:
highlighted
desc order
='1'的所有行
highlighted
desc id
='0'的行
limit 30, 10
- 例如,每页有10个项目)结果应为:
----------------------------
| id | order | highlighted |
----------------------------
| 5 | 9 | 1 |
----------------------------
| 2 | 7 | 1 |
----------------------------
| 1 | 5 | 1 |
----------------------------
| 4 | 0 | 0 |
----------------------------
| 3 | 0 | 0 |
----------------------------
| *and so on* |
----------------------------
有什么想法吗? 提前感谢您的任何建议!
答案 0 :(得分:1)
试试这个:
SELECT a.id, a.order, a.highlighted
FROM tableA a
ORDER BY a.highlighted DESC,
(CASE WHEN a.highlighted = 1 THEN a.order ELSE a.id END) DESC
LIMIT 30, 10;
答案 1 :(得分:0)
尝试以下:
select * from
(
select * from tableA where highlighted = 1 Order By order desc
UNION
select * from tableA where highlighted = 0 Order By id desc
)alias LIMIT 30, 10