【发布时间】:2020-12-10 06:31:54
【问题描述】:
我的数据库
| id | title | create_by |
|---|---|---|
| 1 | aaa | 2 |
| 2 | bbb | 1 |
| 3 | ccc | 3 |
| 4 | ddd | 2 |
| 5 | eee | 2 |
| 6 | fff | 3 |
我想要什么。按具有相同最高值的行排序。
| id | title | create_by |
|---|---|---|
| 1 | aaa | 2 |
| 4 | ddd | 2 |
| 5 | eee | 2 |
| 3 | ccc | 3 |
| 6 | fff | 3 |
| 2 | bbb | 1 |
非常感谢。
【问题讨论】:
标签: mysql
我的数据库
| id | title | create_by |
|---|---|---|
| 1 | aaa | 2 |
| 2 | bbb | 1 |
| 3 | ccc | 3 |
| 4 | ddd | 2 |
| 5 | eee | 2 |
| 6 | fff | 3 |
我想要什么。按具有相同最高值的行排序。
| id | title | create_by |
|---|---|---|
| 1 | aaa | 2 |
| 4 | ddd | 2 |
| 5 | eee | 2 |
| 3 | ccc | 3 |
| 6 | fff | 3 |
| 2 | bbb | 1 |
非常感谢。
【问题讨论】:
标签: mysql
WITH cte AS ( SELECT id,
title,
create_by,
COUNT(create_by) OVER (PARTITION BY create_by) cnt
FROM src_table )
SELECT id,
title,
create_by
FROM cte
ORDER BY cnt, id;
【讨论】:
ORDER BY 子句中允许使用窗口函数。