【发布时间】:2013-05-13 03:20:44
【问题描述】:
查询中:
SELECT
i.id, i.title, i.description,
cities.name as city,
GROUP_CONCAT(DISTINCT station.name) as station,
GROUP_CONCAT(DISTINCT p.url) as photos
FROM
items i
INNER JOIN
cities ON cities.id = i.city_id
LEFT JOIN
item_photos p ON p.item_id = i.id
LEFT JOIN
item_stations s ON s.item_id = i.id
INNER JOIN
stations ON stations.id = s.station_id
WHERE i.id = ?
LIMIT 1
如果表 item_stations 中的行不存在,则两个 LEFT JOIN 都有效:返回照片,为车站返回 NULL。但在这种情况下使用 INNER JOIN 查询将为照片和电台返回 NULL。如果 item_stations 和 s.item_id = i.id 中不需要的行,我应该如何重写查询以说 INNER JOIN 不加入表?
【问题讨论】:
标签: mysql left-join inner-join