代码之家  ›  专栏  ›  技术社区  ›  simon

Oracle pl/sql:引用内部联接查询中的列名

  •  0
  • simon  · 技术社区  · 15 年前

    我有以下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
    (...)
    

    好像不管用。如何实现这一目标?

    2 回复  |  直到 15 年前
        1
  •  1
  •   ozczecho    15 年前

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

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

    然后在主菜单中选择:

    SELECT innertable.autoid ....
    
        2
  •  1
  •   Bob Jarvis - Слава Україні    15 年前

    这就是你想要的吗?

    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'