在内部联接期间两次使用相同的表数据时出错

时间:2013-11-21 09:10:07

标签: sql sql-server-2008 inner-join

假设我有一个数据库名称为'Old'的表,如下所示:

TABLE A
(
    SeniorVehicle varchar(255),
    SeniorVehicleAllowance int,
    JuniorVehicle varchar(255),
    JuniorVehicleAllowance int
    ManagerVehicle varchar(255),
    ManagerVehicleAllowance int

);

另一个表格,数据库名称为“新建”,如下所示:

TABLE B
(
    SeniorVehicle int,
    SeniorVehicleAllowance int,
    JuniorVehicle int,
    JuniorVehicleAllowance int,
    ManagerVehicle int,
    ManagerVehicleAllowance int
);

我想将数据库'Old'的表A中的数据带到数据库'New'的表B中。

问题是,两个数据库中都有一个名为Vehicle的表格如下:

TABLE Vehicle
(
    VehicleID int pk,
    VehicleName varchar(255)
)

表A中的SeniorVehicle,JuniorVehicle和ManagerVehicle列中的值是TABLE Vehicle中的VehicleName值。但必须存储在表B中的SeniorVehicle,JuniorVehicle和ManagerVehicle的值必须是Vehicle Table中的VehicleID列的值。

如何在没有错误的情况下实现这一点????

我尝试了以下内容:

INSERT INTO B
(SeniorVehicle, SeniorVehicleAllowance, JuniorVehicle, JuniorniorVehicleAllowance, ManagerVehicle, ManagerVehicleAllowance)

SELECT Vehicle.VehicleID, c.SeniorVehicleAllowance, c.VehicleID, c.JuniorVehicleAllowance, c.VehicleID, c.ManagerVehicleAllowance

FROM (SELECT b.SeniorVehicle, b.SeniorVehicleAllowance, Vehicle.VehicleID, b.JuniorVehicleAllowance, b.VehicleID, b.ManagerVehicleAllowance

FROM (SELECT a.SeniorVehicle, a.SeniorVehicleAllowance, a.JuniorVehicle, a.JuniorVehicleAllowance, Vehicle.VehicleID, a.ManagerVehicleAllowance

FROM (SELECT SeniorVehicle, SeniorVehicleAllowance, JuniorVehicle, JuniorVehicleAllowance, ManagerVehicle, ManagerVehicleAllowance FROM A) as a

Inner join
Vehicle

ON a.ManagerVehicle = Vehicle.VehicleName) as b

Inner join
Vehicle

ON b.JuniorVehicle = Vehicle.VehicleName) as c

Inner join
Vehicle

ON c.SeniorVehicle = Vehicle.VehicleName

我收到以下错误:

The column 'VehicleID' was specified multiple times for 'c'

我的数据库是MSSQL Server 2008 R2

1 个答案:

答案 0 :(得分:1)

重新格式化当前查询会显示:

SELECT
            Vehicle.VehicleID, 
            c.SeniorVehicleAllowance, 
            c.VehicleID, 
            c.JuniorVehicleAllowance, 
            c.VehicleID, 
            c.ManagerVehicleAllowance

FROM (
    SELECT      b.SeniorVehicle, 
                b.SeniorVehicleAllowance, 
                Vehicle.VehicleID, 
                b.JuniorVehicleAllowance, 
                b.VehicleID, 
                b.ManagerVehicleAllowance

    FROM (
        SELECT      a.SeniorVehicle, 
                    a.SeniorVehicleAllowance, 
                    a.JuniorVehicle, 
                    a.JuniorVehicleAllowance, 
                    Vehicle.VehicleID, 
                    a.ManagerVehicleAllowance

        FROM (
            SELECT      SeniorVehicle, 
                        SeniorVehicleAllowance, 
                        JuniorVehicle, 
                        JuniorVehicleAllowance, 
                        ManagerVehicle, 
                        ManagerVehicleAllowance 
            FROM A
        ) as a

        Inner join  Vehicle
            ON      a.ManagerVehicle = Vehicle.VehicleName
    ) as b

    Inner join  Vehicle
        ON      b.JuniorVehicle = Vehicle.VehicleName
) as c

Inner join  Vehicle
    ON      c.SeniorVehicle = Vehicle.VehicleName

在此查询中,子查询别名c有两列名为VehicleID(这是您的错误消息告诉您的。

解决问题的最小变化是对子查询中的列进行别名,例如:

SELECT
            Vehicle.VehicleID AS SeniorVehicleId, 
            c.SeniorVehicleAllowance, 
            c.JuniorVehicleId, 
            c.JuniorVehicleAllowance, 
            c.ManagerVehicleID, 
            c.ManagerVehicleAllowance

FROM (
    SELECT      b.SeniorVehicle, 
                b.SeniorVehicleAllowance, 
                Vehicle.VehicleID AS JuniorVehicleId, 
                b.JuniorVehicleAllowance, 
                b.ManagerVehicleID, 
                b.ManagerVehicleAllowance

    FROM (
        SELECT      a.SeniorVehicle, 
                    a.SeniorVehicleAllowance, 
                    a.JuniorVehicle, 
                    a.JuniorVehicleAllowance, 
                    Vehicle.VehicleID AS ManagerVehicleID, 
                    a.ManagerVehicleAllowance
-- Rest ommited for brevity

也可以使用更多连接重新编写查询,并且也完全省略子查询的需要:

SELECT      srmgr.VehicleId AS SeniorVehicleId, 
            A.SeniorVehicleAllowance, 
            jrmgr.VehicleId AS JuniorVehicleId, 
            A.JuniorVehicleAllowance, 
            mgr.VehicleId AS ManagerVehicleId, 
            A.ManagerVehicleAllowance 
FROM A

INNER JOIN  Vehicle AS mgr
    ON      a.ManagerVehicle = mgr.VehicleName

INNER JOIN  Vehicle AS jrmgr
    ON      a.ManagerVehicle = jrmgr.VehicleName

INNER JOIN  Vehicle AS srmgr
    ON      a.ManagerVehicle = srmgr.VehicleName