gpt4 book ai didi

MySQL 最低排序和分组依据

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

表格数据:从用户中选择firstName、package_id、网站

请注意,表中有超过 30k 行。

enter image description here

我试图在每个网站中显示最低的package_id,这里是查询

select firstName, package_id, website from user group by website order by package_id

enter image description here

它应该显示每个网站的最低 package_id,但它不正确,例如对于值 kahuta 它应该返回 6 和对于 Null => 10example.com => 9

最佳答案

如果不存在:

select u.* from user u
where not exists (
select 1 from user
where website <=> u.website and package_id < u.package_id
)

或将表连接到子查询,该子查询返回每个网站min package_id:

select u.* 
from user u inner join (
select website, min(package_id) minpackageid
from user
group by website
) g on g.website <=> u.website and g.minpackageid = u.package_id

关于MySQL 最低排序和分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58456029/

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