gpt4 book ai didi

MySQL 按子查询总和排序

转载 作者:行者123 更新时间:2023-11-30 01:16:29 28 4
gpt4 key购买 nike

我试图从表中选择一组行并按一组子查询进行排序:

SELECT *, 
(SELECT Count(id) > 0 FROM places_users WHERE places_id = places.id) AS verified_bool,
(SELECT Count(id) > 0 FROM places_services WHERE places_id = places.id) AS services_bool
FROM places
ORDER BY sum(verified_bool + services_bool) DESC
LIMIT 0, 10

但它只返回 1 个结果。子查询检查连接到原始行的其他表。我只需要验证其他表是否至少有 1 个结果,因此我使用 bool 值,然后尝试对子查询 0 或 1 求和,并将原始结果从总和最大的结果排列到总和最小的结果。

最佳答案

问题在于 order by 子句中的总和:

SELECT *, 
(SELECT Count(id) > 0 FROM places_users WHERE places_id = places.id) AS verified_bool,
(SELECT Count(id) > 0 FROM places_services WHERE places_id = places.id) AS services_bool
FROM places
ORDER BY (verified_bool + services_bool) DESC
LIMIT 0, 10

关于MySQL 按子查询总和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19006238/

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