Sql 在id上合并2个表
我有两张桌子——表一和表二Sql 在id上合并2个表,sql,Sql,我有两张桌子——表一和表二 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 我需要如下输出。ie,表2中id的对应名
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
我需要如下输出。ie,表2中id的对应名称
OUTPUT
city district state nation
c1 d1 s2 n1
c2 d1 s2 n1
c3 d2 s1 n2
是否可以编写简单的sql查询来获得上述输出
怎么做
谢谢州应该是s2吗?是的。s2 s1。对不起,我弄错了
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
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;