我有两张桌子。
表1:
ID | Statecode
---------------------
1 | VA
2 | TX
表2:
b_state | m_state
------------------
2 | 1
1 | 2
我想写一个查询,它将为我提供如下值的结果:
输出:
bus_state | mail_state
-----------------------
TX | VA
VA | TX
我应该使用什么连接来获得结果?
答案 0 :(得分:1)
您需要将Table 1
两次加入Table 2
:
SELECT b.Statecode AS bus_state, m.Statecode AS mail_state
FROM `Table 2` t2
JOIN `Table 1` b ON b.ID = t2.b_state
JOIN `Table 1` m ON m.ID = t2.m_state
在sqlfiddle上查看。