我有一个像这样的表:
数据:
id | state | more
-----------------
1 | 1 | ...
2 | 2 | ...
3 | 1 | ...
和州:
id | state
----------
1 | Open
2 | Closed
如果我这样做
SELECT states.state FROM data LEFT JOIN states ON data.state = states.id
我收到了
state
-----
Open
Closed
Open
到目前为止一切顺利。我想做的事情(没有成功)是,如果我有一个像这样的数据表:
id | state | state2 | more
--------------------------
1 | 1 | 2 | ...
2 | 2 | 1 | ...
3 | 1 | 1 | ...
我如何获得:
state | state 2
----------------
Open | Closed
Closed | Open
Open | Open
答案 0 :(得分:1)
SELECT states.state, states2.state
FROM data
LEFT JOIN states as states ON data.state = states.id
LEFT JOIN states as states2 ON data.state2 = states2.id
答案 1 :(得分:1)
只需使用其他LEFT JOIN
和alias
SELECT
s1.state AS state1
s2.state AS state2
FROM data d
LEFT JOIN states s1 ON d.state = s1.id
LEFT JOIN states s2 ON d.state2 = s2.id
答案 2 :(得分:0)
你需要做另一个左连接
SELECT
s1.state AS state1,
s2.state AS state2
FROM data d
LEFT JOIN states s1 ON d.state = s1.id
LEFT JOIN states s2 ON d.state2 = s2.id