首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用join从数据库中查找country wise所有州和州wise城市

使用join从数据库中查找country wise所有州和州wise城市
EN

Stack Overflow用户
提问于 2016-10-06 18:07:58
回答 3查看 2.4K关注 0票数 0

使用join从数据库中查找country wise所有州和州wise城市

我在数据库中有三个表,即国家,州,城市,我使用主键和外键给出了它们之间的映射。我想要具体的“印度”国家和相关的国家,所有的邦和cities...How我能做吗?在这里我给我的code...which I‘m created..Give me任何其他建议

代码语言:javascript
复制
select countries.name as country,
states.name as state,
cities.name as city
from states inner join cities
on states.id = cities.state_id
inner join countries
WHERE countries.name='India';
EN

回答 3

Stack Overflow用户

发布于 2016-10-06 18:14:32

请使用以下查询

具有以下数据的表-

代码语言:javascript
复制
+------+-------+
| id   | name  |
+------+-------+
|    1 | india |
|    2 | pak   |
|    3 | afgan |
+------+-------+

状态表

代码语言:javascript
复制
+------+---------+------------+
| id   | name    | country_id |
+------+---------+------------+
|    1 | haryana |          1 |
|    2 | gujrat  |          1 |
|    3 | himchal |          2 |
+------+---------+------------+

城市表

代码语言:javascript
复制
+------+-------------+----------+
| id   | name        | state_id |
+------+-------------+----------+
|    1 | bahadurgarh |        1 |
|    2 | hisar       |        1 |
|    3 | surat       |        2 |
|    4 | shimla      |        3 |
+------+-------------+----------+




select cities.id , cities.name ,states.id , states.name ,countries.name from cities left join states on cities.state_id = states.id left join countries on states.country_id = countries.id where countries.name='india' ;

+------+-------------+------+---------+-------+
| id   | name        | id   | name    | name  |
+------+-------------+------+---------+-------+
|    1 | bahadurgarh |    1 | haryana | india |
|    2 | hisar       |    1 | haryana | india |
|    3 | surat       |    2 | gujrat  | india |
+------+-------------+------+---------+-------+
票数 1
EN

Stack Overflow用户

发布于 2016-10-06 18:19:11

从sql中找出country wise state和state wise city不是一个好主意。

你应该用javascript试试这个just include this file in your required webpage

票数 0
EN

Stack Overflow用户

发布于 2016-10-06 18:25:25

尝试以下操作(很抱歉代码中没有换行符。不知道它是如何工作的)。

代码语言:javascript
复制
Select countries.name as country, states.name as sta from countries left join cities on states.id= cities.state_id where states.country_id in (SELECT countries.id FROM countries where countries.name='India') 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39893174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档