我正在将一个相当简单的查询与 JOIN 语句中的子查询组合在一起。仅当我在子查询选择中包含 * 时才有效。为什么?
这有效
$sql = 'SELECT locations.id, title, name, hours.lobby
FROM locations
LEFT JOIN states ON states.id = locations.state_id
LEFT JOIN (SELECT *, type_id IS NOT NULL AS lobby FROM location_hours) AS hours ON locations.id = hours.location_id
GROUP BY locations.id';
这不
$sql = 'SELECT locations.id, title, name, hours.lobby
FROM locations
LEFT JOIN states ON states.id = locations.state_id
LEFT JOIN (SELECT type_id IS NOT NULL AS lobby FROM location_hours) AS hours ON locations.id = hours.location_id
GROUP BY locations.id';
我什至应该这样做吗?如果您不需要所有字段,我认为 * 不是最好的?