gpt4 book ai didi

MySQL GROUP BY 和 ORDER BY 的使用

转载 作者:行者123 更新时间:2023-11-29 01:06:56 25 4
gpt4 key购买 nike

我有一张人表,每个人都有访问次数优先级:

ID | NAME          |  VISITS | PRIORITY
---------------------------------------
1 | John | 1 | 0
2 | Joe | 2 | 1
3 | Peter | 1 | 1
4 | Sara | 0 | 2
5 | Phillip | 5 | 0
6 | Bob | 3 | 1
7 | Andrew | 4 | 2

只有某些允许的优先级:(0,1,2)

如何创建一个查询,根据访问次数(从最低开始)为我提供在相应优先级组内的人员?

所以结果应该是这样的:

ID | NAME          |  VISITS | PRIORITY
---------------------------------------
1 | John | 1 | 0
2 | Philip | 5 | 0
3 | Peter | 1 | 1
4 | Joe | 2 | 1
5 | Bob | 3 | 1
6 | Sara | 0 | 2
7 | Andrew | 4 | 2

我想必须使用 GROUP BY 子句,但我不确定如何使用它。

最佳答案

你刚刚试过 ORDER BY:

select id, name, visits, priority
from people
order by priority, visits

参见 SQL Fiddle with Demo

关于MySQL GROUP BY 和 ORDER BY 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12936871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com