SELECT
p.name,
COUNT(DISTINCT c.id) AS city_count
FROM
residences AS r
INNER JOIN
cities AS c
ON c.id = r.city_id
INNER JOIN
provinces AS p
ON p.id = r.province_id
WHERE
r.status = 1
AND c.status = 1
AND p.status = 1
GROUP BY
p.name
或者...
SELECT
p.name,
COUNT(*) AS city_count
FROM
cities AS c
INNER JOIN
provinces AS p
ON p.id = c.province_id
WHERE
c.status = 1
AND p.status = 1
AND EXISTS (
SELECT *
FROM residences
WHERE status = 1
AND city_id = c.id
AND province_id = p.id
)
GROUP BY
p.name
SELECT
p.name,
COUNT(DISTINCT c.id) AS city_count
FROM
residences AS r
INNER JOIN
cities AS c
ON c.id = r.city_id
INNER JOIN
provinces AS p
ON p.id = r.province_id
WHERE
r.status = 1
AND c.status = 1
AND p.status = 1
GROUP BY
p.name
Or...
SELECT
p.name,
COUNT(*) AS city_count
FROM
cities AS c
INNER JOIN
provinces AS p
ON p.id = c.province_id
WHERE
c.status = 1
AND p.status = 1
AND EXISTS (
SELECT *
FROM residences
WHERE status = 1
AND city_id = c.id
AND province_id = p.id
)
GROUP BY
p.name
发布评论
评论(1)
或者...
Or...