国家和城市的国家明智数据

时间:2019-10-17 06:31:59

标签: sql-server sql-server-2008 sql-server-2012

我有这些表:

enter image description here

预期输出: enter image description here

3 个答案:

答案 0 :(得分:1)

使用简单的联接即可实现:

select s.name,c.name,ci.name from @state s
join @Country c
on s.countryid = c.id
join @city Ci on ci.stateid=s.ID

答案 1 :(得分:0)

简单的左联接将为您工作:

SELECT 
    S.Name AS State, 
    C.Name AS Country,
    CT.Name AS City
FROM 
    State S 
LEFT JOIN Country C 
    ON C.Id = S.Country_Id 
LEFT JOIN City CT
    ON CT.StateId = S.Id 

答案 2 :(得分:0)

使用join可以实现它。

select ,b.name a.name,c.name Country as a inner join State as b on a.id=b.CountryID inner join City as c on c.StateId=b.id