对不起这个可怕的头衔,不知道怎么解释这个。我有一个看起来像这样的查询:
SELECT `name`, `latitude`, `longitude`, `indoor`, `address`,
`phone`, `email`, `website`, `closed`
FROM rocks INNER JOIN
(SELECT DISTINCT id FROM rock_types WHERE type="DWS" or type="Top rope")
AS types ON rocks.id=types.id
WHERE (latitude>-180 AND latitude<180) AND (longitude>-180 AND longitude<180)
除了我现在所获得的内容之外,我还想获得与每个id相关联的类型列表。我怎样才能做到这一点?感谢
答案 0 :(得分:1)
以下是使用传统连接语法的方法:
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
答案 1 :(得分:0)
SELECT `name`, `latitude`, `longitude`, `indoor`, `address`,
`phone`, `email`, `website`, `closed`,
(SELECT GROUP_CONCAT(type SEPARATOR ',') FROM rock_types
WHERE (type="DWS" or type="Top rope") and rock_types.id=rocks.id group by id) types
FROM rocks
WHERE (latitude>-180 AND latitude<180) AND (longitude>-180 AND longitude<180)