MySQL按子查询的总和顺序

时间:2013-09-25 13:26:54

标签: mysql sql

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

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个结果,所以我使用布尔值,然后尝试总结子查询0或1,并将最大总和的原始结果排列到最少的那些。

1 个答案:

答案 0 :(得分:0)

问题在于顺序中的总和:

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