MySQL:多个选择,多个连接?

时间:2012-01-13 22:02:17

标签: mysql select join

我有一组多个表(剥离概述):

Brand;
+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| id                 | tinyint(10)  | NO   | PRI | NULL    |       |
| name               | varchar(255) | NO   |     | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+

Relations;
+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| id1                | tinyint(10)  | NO   | PRI | NULL    |       |
| id2                | tinyint(10)  | NO   | PRI | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+

Country;
+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| id                 | tinyint(10)  | NO   | PRI | NULL    |       |
| name               | varchar(255) | NO   |     | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+

City;
+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| id                 | tinyint(10)  | NO   | PRI | NULL    |       |
| name               | varchar(255) | NO   |     | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+

每一行都有一个唯一的ID。品牌表中的每一行都与国家和城市有关系。这些表之间的关系存储在Relations表中,因此每个品牌行的关系表中有两行。

我正在尝试获取给定品牌ID的品牌名称,国家/地区名称和城市名称。我可以通过个人选择来选择这些关系,但我的目标是在单个查询中进行(如果可能)。

我能够获得其中一个关系(国家),但我也希望得到这个城市:

SELECT Brand.name as brand, Country.name as country
FROM Brand 
LEFT JOIN _relations 
ON Relations.id1 = Brand.id 
INNER JOIN Country 
ON Country.id = Relations.id2 
WHERE Brand.id = '123456';

结果:

+-----------+-------------+
| brand     | country     |
+-----------+-------------+
| brandname | countryname |
+-----------+-------------+

我的目标是获得:

+-----------+-------------+-------------+
| brand     | country     | city        |
+-----------+-------------+-------------+
| brandname | countryname | cityname    |
+-----------+-------------+-------------+

任何人都可以帮我这个或者指出我正确的方向吗?

2 个答案:

答案 0 :(得分:0)

再次使用不同的别名加入“关系”表。

这样的事情:

SELECT Brand.name as brand, Country.name as country, City.name as city
FROM Brand 
LEFT JOIN Relations ON Relations.id1 = Brand.id 
INNER JOIN Country ON Country.id = Relations.id2 
LEFT JOIN Relations r2 ON r2.id1 = Brand.id
LEFT JOIN City ON City.id = r2.id2
WHERE Brand.id = '123456';

答案 1 :(得分:0)

基本上你再次加入关系,然后使用它来获得城市,所以像

SELECT Brand.name as brand, Country.name as country, City.Name as City
FROM Brand 
LEFT JOIN _relations relCountry
ON relCountry.id1 = Brand.id 
INNER JOIN Country 
ON Country.id = relCountry.id2 
LEFT JOIN _relations relCity 
ON relCity.id1 = Brand.id 
INNER JOIN City 
ON City.id = relCity.id2