Please refer to my earlier question - MySQL query - complex searching condition推荐答案您可以使用基本查询来获取所有property.id(并对它们进行计数)和SELECT列表中的子查询以计算其他条件,例如:You can use your base query to get all property.id (and count them) and subqueries in the SELECT list to count the additional conditions, like this:SELECT COUNT(*) AS BaseCount , ( SELECT COUNT(*) FROM location AS l3 WHERE l3.property_id = p.id AND l3.location_type = 3 ) AS CountLocation3 , ( SELECT COUNT(*) FROM location AS l4 WHERE l4.property_id = p.id AND l4.location_type = 4 ) AS CountLocation4 , ( SELECT COUNT(*) FROM amenities AS a2 WHERE a2.property_id = p.id AND a2.amenity_type = 2 ) AS CountAmenity4 , ...FROM property AS p JOIN location AS l1 ON l1.property_id = p.id AND l1.location_type = 1 JOIN location AS l2 ON l2.property_id = p.id AND l2.location_type = 2 JOIN amentities AS a1 ON a1.property_id = p.id AND a1.amenity_type = 1 JOIN properties AS p3 ON p3.property_id = p.id AND p3.property_type = 3 JOIN properties AS p1 ON p1.property_id = p.id AND p1.property_type = 1 这篇关于MySQL查询-复杂计数条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 10:12