如何从此数据库中获取某个人的国家/地区?

时间:2016-07-16 07:42:49

标签: mysql select

我在使用给定数据库,在Mysql上使用假模拟器进行分配时遇到了非常糟糕的时间。我的问题是,我需要一个Query来从客户的国家/地区获取数据,但客户的表只包含City数据。表格是:

tbl_Client:
/ ID*
/ Name
/ Birthday
/ ID_City (FK)

tbl_City:
/ ID_City *
/ vchCityName
/ ID_State (FK)

tbl_State:
/ ID_State *
/ vchStateName
/ ID_intRegion (FK)

tbl_Region:
/ ID_Region *
/ vchRegionName
/ ID_Country (FK)

tbl_Country:
/ ID_Country *
/ vchCountryName

所以,我的问题是,如何让客户的国家/地区与其他数据进行比较? (最大的问题是将客户的国籍与他们喜欢的音乐家进行比较,但我似乎无法管理的是客户部分)。我正在尝试一些非常基本的东西,但它不起作用。

(...)tbl_Client CL, tbl_City CT, tbl_State ST, tbl_Region R, tbl_Country C
WHERE CT.ID_City = ST.ID_City 
AND ST.ID_Region = R.ID_Region 
AND R.ID_Contry = C.ID_Contry

然后我试着比较那个C.ID_Country,好吧,我尝试了很多东西但仍然什么都没有......如果有人能在这方面给我启发,我真的很感激。

非常感谢大家!

1 个答案:

答案 0 :(得分:0)

通过这种方式,您可以获得关系中的所有数据

select tbl_Client.*, tbl_City.*, tbl_State.*, tbl_Region.*, tbl_Country.*
from tbl_Client
inner join tbl_City on tbl_Client.ID_City = tbl_City.ID_City
inner join tbl_State on tbl_State.ID_State = tbl_City.ID_State
INNER join tbl_Region on tbl_Region.ID_Region = tbl_State.ID_Region
INNER join tbl_Country on tbl_Country.ID_Country =tbl_Region.ID_Country; 

如果您需要国家/地区的客户ID,例如id = 3

select tbl_Client.* , tbl_Country.*
from tbl_Client
inner join tbl_City on tbl_Client.ID_City = tbl_City.ID_City
inner join tbl_State on tbl_State.ID_State = tbl_City.ID_State
INNER join tbl_Region on tbl_Region.ID_Region = tbl_State.ID_Region
INNER join tbl_Country on tbl_Country.ID_Country =tbl_Region.ID_Country
where tbl_Client.ID = 3;