我有2张桌子 - TABLE1& TABLE2
TABLE1
city district state nation
1 27 37 41
3 27 37 41
6 29 32 43
TABLE2
id name
1 c1
3 c2
6 c3
27 d1
29 d2
32 s1
37 s2
41 n1
43 n2
我需要输出如下。即,来自TABLE2
的id的相应名称OUTPUT
city district state nation
c1 d1 s2 n1
c2 d1 s2 n1
c3 d2 s1 n2
是否可以编写简单的sql查询来获得上述输出?
如何??
由于
答案 0 :(得分:0)
select city.name as city
, district.name as distict
, state.name as state
, nation.name as nation
from table1 t1
inner join table2 city on city.id = t1.city
inner join table2 state on state.id = t1.state
inner join table2 district on district.id = t1.district
inner join table2 nation on nation.id = t1.nation
答案 1 :(得分:0)
SELECT
c.name as city,
d.name as district,
s.name as state,
n.name as nation
FROM TABLE1 t1
JOIN TABLE2 c on t1.city=c.id
JOIN TABLE2 d on t1.district=d.id
JOIN TABLE2 s on t1.state=s.id
JOIN TABLE2 n on t1.nation=n.id;