如何从mysql中的3个表中获取数据?

时间:2013-07-19 19:20:36

标签: mysql sql

Location table
-----------------------------------------------
|location.id|country.id|state.id|city.id|

Country
-----------------------------------------------
|country.id|country|

State
-----------------------------------------------
|state.id|state|

City
-----------------------------------------------
|city.id|city|

现在我有location.id,我需要从location.id一次获取国家,城市,州名。

需要有关Sql查询的帮助来解决这个问题

2 个答案:

答案 0 :(得分:1)

这是未经测试的,但您可能想尝试这样的事情,并查找inner joins

select * from locations
inner join Country on `Country`.`country.id` = locations.`country.id`
inner join State on `State`.`state.id` = locations.`state.id`
inner join City on `City`.`city.id` = locations.`city.id`

答案 1 :(得分:0)

SELECT location.id, city.city, state.state,country.country
FROM location,city,state,country
WHERE location.country_id = country.id
AND location.city_id = city.id
AND location.state_id = state.id