我的mysql中有这个表:
| id | category_id | region_id | score |
+----+-------------+-----------+-------+
| 1 | 1 | 1 | 78 |
| 2 | 1 | 2 | 65 |
| 3 | 1 | 3 | 98 |
| 4 | 1 | 4 | 45 |
| 5 | 1 | 5 | 78 |
| 6 | 1 | 1 | 98 |
| 7 | 1 | 2 | 32 |
| 8 | 1 | 3 | 56 |
| 9 | 1 | 4 | 89 |
| 10 | 1 | 5 | 65 |
+----+-------------+-----------+-------+
我想获得5个最新的id,但是根据区域ID命令我的结果表,所以我使用了这个代码
SELECT *
FROM tb_scores
WHERE category_id = 1
ORDER
BY id DESC
, region_id ASC
LIMIT 5
但是结果只将id作为desc而不是region_id作为ASC进行排序。简要解释一下我想要这种结果。
| id | category_id | region_id | score |
+----+-------------+-----------+-------+
| 6 | 1 | 1 | 98 |
| 7 | 1 | 2 | 32 |
| 8 | 1 | 3 | 56 |
| 9 | 1 | 4 | 89 |
| 10 | 1 | 5 | 65 |
+----+-------------+-----------+-------+
答案 0 :(得分:3)
试试这个:
SELECT *
FROM (SELECT *
FROM tb_scores
WHERE category_id = 1
ORDER BY id DESC,region_id ASC LIMIT 5) t
ORDER BY region_id
在子查询中获取数据并在子查询结果上应用order by region_id
。