更新时间:2023-01-29 08:37:33
您可以使用基本查询来获取所有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