Oracle PL/SQL:引用内部连接查询中的列名

发布于 2024-08-20 13:19:35 字数 1186 浏览 3 评论 0原文

我有以下 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 | material | ID_1 | material_1 |
-------------------------------------
| 1  | leather  | 4    | plastic    |
| 2  | leather  | 7    | plastic    |
-------------------------------------

但是,我只想输出 ID 列(而不是材料列),如下所示:


-------------
| ID | ID_1 | 
-------------
| 1  |  4   |
| 2  |  7   | 
-------------

我无法执行此操作,因为我还没有找到在任何查询中引用内部查询的 ID 列的方法。方式。例如


SELECT id, innertable.id
(...)


SELECT id, auto.id
(...)

or


SELECT id, id_1
(...)

似乎不起作用。如何才能实现这一点呢?

I have the following SQL statement:


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'

This query delivers the columns from the table "Cars" twice, but with different values for the ID from the Car table (the purpose of the query is to map the values to see what the Car.ID would be id the material would change from leather to plastic).


------------------------------------
| ID | material | ID_1 | material_1 |
-------------------------------------
| 1  | leather  | 4    | plastic    |
| 2  | leather  | 7    | plastic    |
-------------------------------------

However, I would like to output only the ID columns (not the material columns), like this:


-------------
| ID | ID_1 | 
-------------
| 1  |  4   |
| 2  |  7   | 
-------------

I have not been able to do this, as I haven't found a way to refer to the ID column of the inner query in any way. For example


SELECT id, innertable.id
(...)

or


SELECT id, auto.id
(...)

or


SELECT id, id_1
(...)

don't seem to work. How can achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澜川若宁 2024-08-27 13:19:35

尝试在 select 语句中显式列出内表的列名。 Like:

...(SELECT auto.ID autoid, auto.Whatever)....

然后在主选择中:

SELECT innertable.autoid ....

Try explicitly listing the column names of the inner table in the select statement. Like:

...(SELECT auto.ID autoid, auto.Whatever)....

and then in the main select:

SELECT innertable.autoid ....
逐鹿 2024-08-27 13:19:35

这就是你所追求的吗?

SELECT auto_id, steeringwheel_id
  FROM cars car 
       LEFT JOIN (SELECT auto.ID AS auto_id, sw1.id AS steeringwheel_id
                  FROM   cars auto 
                         LEFT JOIN steeringwheels sw1
                           ON auto.steeringwheelid = sw1.ID 
                  WHERE  material = 'leather') innertable 
         ON innertable.auto_ID = car.ID 
       LEFT JOIN steeringwheels sw2
         ON auto.steeringwheelid = sw2.ID 
  WHERE sw.material='plastic' 

Is this what you're after?

SELECT auto_id, steeringwheel_id
  FROM cars car 
       LEFT JOIN (SELECT auto.ID AS auto_id, sw1.id AS steeringwheel_id
                  FROM   cars auto 
                         LEFT JOIN steeringwheels sw1
                           ON auto.steeringwheelid = sw1.ID 
                  WHERE  material = 'leather') innertable 
         ON innertable.auto_ID = car.ID 
       LEFT JOIN steeringwheels sw2
         ON auto.steeringwheelid = sw2.ID 
  WHERE sw.material='plastic' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文