【发布时间】:2014-08-25 05:38:06
【问题描述】:
我有几张桌子。
table_0
id | id_table_1 |id_table_2 | people_id
1 1 0 1
2 0 2 1
3 0 0 1
4 0 1 2
table_1
id | machine| type
1 bmw 1
2 reno 1
....
table_2
id | machine |type
1 yamaha 2
2 ducati 3
....
table_3(类型)
id | name
1 auto
2 bike
3 sportbike
....
我想做一个可以得到这个结果的选择查询
tabel_0.id | table_0.people_id | machine(table_1 or table_2) | type
1 1 bmw auto
2 1 ducati sportbike
3 1 "" ""
上次我问过这个问题,但没有行类型,现在我有这个代码
SELECT table_0.id, table_0.people_id,
IFNULL(table_1.machine, table_2.machine) AS machine
FROM table_0
LEFT JOIN table_1 ON table_0.id_table_1 = table_1.id
LEFT JOIN table_2 ON table_0.id_table_2 = table_2.id
WHERE table_0.people_id = 1
请帮助解决我的问题。谢谢!
【问题讨论】: