我有以下SQL语句:
SELECT *
FROM cars car
LEFT JOIN (SELECT *
FROM cars auto
LEFT JOIN steeringwheels sw
ON auto.steeringwheelid = sw.ID
WHERE material = 'leather') innertable
ON innertable.ID = car.ID
LEFT JOIN steeringwheels sw
ON auto.steeringwheelid = sw.ID
WHERE sw.material='plastic'
此查询两次传递表“cars”中的列,但car表中id的值不同(查询的目的是映射值以查看car.id是什么id,材质将从皮革更改为塑料)。
| ID | material | ID_1 | material_1 |
| 1 | leather | 4 | plastic |
| 2 | leather | 7 | plastic |
但是,我只想输出ID列(而不是Material列),如下所示:
| ID | ID_1 |
| 1 | 4 |
| 2 | 7 |
我无法做到这一点,因为我找不到任何方法来引用内部查询的ID列。例如
SELECT id, innertable.id
(...)
或
SELECT id, auto.id
(...)
或
SELECT id, id_1
(...)
好像不管用。如何实现这一目标?