gpt4 book ai didi

mysql - 如何使用子查询中的值按记录排序

转载 作者:行者123 更新时间:2023-11-29 22:10:29 24 4
gpt4 key购买 nike

我的查询看起来像这样

SELECT 
users.*,
(select count(*)
from user_actions
where status='pending' and user_id = users.id) number_pending
FROM users
order by name

我需要首先显示 number_pending 计数大于零的记录。您能帮我实现这一目标吗?

最佳答案

SELECT 
u.*, count(a.*) number_pending
FROM users u join user_actions a
on a.user_id = u.id
where a.status='pending'
order by count(a.*) desc

这就是你想做的事吗?

关于mysql - 如何使用子查询中的值按记录排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31724196/

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