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

标签 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 查询帮助来解决此问题

最佳答案

这尚未经过测试,但您可能想尝试类似的操作,并查找 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`

关于mysql - 如何从mysql中的3个表中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17753911/

相关文章:

sql - 当比较值由两个单独的字段组合时,是否可以使用单个 SQL 获取表中的最小(最大)行?

mysql - 无法再在 Debian 8 中安装 MySQL 8

mysql - 如何在 knex 中使用 'and' 和 'on' 连接条件

php - SQL语法错误,我就是看不出来

mysql - 循环遍历sql查询结果

mysql - 在MySQL中将3列表转为二维矩阵

mysql - 类多态 SQL 数据库表结构

sql - mysql为非主键列分配自动编号

mysql - 使用 select 子句更新 mysql 作为更新值

java - Oracle - 组合字段和搜索 - 性能