我有一个这样的表:

数据:

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

最佳答案

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

关于mysql - MySQL Left Join不止一次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45377058/

10-14 02:16