下面是使用傳統的連接語法我的看法:
SELECT rocks.name,
rocks.latitude,
rocks.longitude,
rocks.indoor,
rocks.address,
rocks.phone,
rocks.email,
rocks.website,
rocks.closed,
rock_types.type
FROM rocks,
rock_types
WHERE rocks.latitude > -180 AND
rocks.latitude < 180 AND
rocks.longitude > -180 AND
rocks.longitude < 180 AND
rocks.type_id = rocks_types.id AND
rock_types.type IN ('DWS', 'Top rope')
的rocks.id = rocks_types.id
做兩個表之間的聯繫。
這裏是你如何編寫使用INNER JOIN
相同的查詢(既導致了同樣的結果)
SELECT rocks.name,
rocks.latitude,
rocks.longitude,
rocks.indoor,
rocks.address,
rocks.phone,
rocks.email,
rocks.website,
rocks.closed,
rock_types.type
FROM rocks
INNER JOIN rocks_types ON rocks.type_id = rocks_types.id
WHERE rocks.latitude > -180 AND
rocks.latitude < 180 AND
rocks.longitude > -180 AND
rocks.longitude < 180 AND
rock_types.type IN ('DWS', 'Top rope')
編輯:基於您的評論,下面會給你一個逗號分隔的類型列表:
SELECT rocks.name,
rocks.latitude,
rocks.longitude,
rocks.indoor,
rocks.address,
rocks.phone,
rocks.email,
rocks.website,
rocks.closed,
(SELECT GROUP_CONCAT(DISTINCT rock_types.type SEPARATOR ',')
FROM rock_types
WHERE rock_types.id = rocks.id AND
rock_types.type IN ('DWS', 'Top rope')
GROUP BY rock_types.id
) AS type
FROM rocks
WHERE rocks.latitude > -180 AND
rocks.latitude < 180 AND
rocks.longitude > -180 AND
rocks.longitude < 180
那麼你的標題應該是這樣的,「獲得的與SQL每個ID相關聯的列表類型」 – random 2009-08-10 04:04:37