我一直收到错误“列-column name-多次指定-table name-”

时间:2013-11-20 20:51:41

标签: sql

CREATE VIEW CustomerAddress
AS
SELECT C.CustomerID, C.EmailAddress, C.LastName, C.FirstName, B.Line1, B.Line2, B.City, B.State, B.ZipCode, S.Line1, S.Line2, S.City, S.State, S.ZipCode 
FROM Customers C JOIN Addresses S ON C.ShippingAddressID = S.AddressID, Customers U JOIN Addresses B ON U.BillingAddressID = B.AddressID;

5 个答案:

答案 0 :(得分:2)

在视图中,两列不能有相同的名称。在您的查询中,B.Line1和S.Line1具有与其他任何名称相同的名称。您需要将其中一个更改为与其他一个不同。

CREATE VIEW CUSTOMERADDRESS
AS
SELECT C.CUSTOMERID, C.EMAILADDRESS, C.LASTNAME, C.FIRSTNAME, B.LINE1 AS BLINE1, B.LINE2 AS AS BLINE2, B.CITY AS BCITY, B.STATE AS BSTATE, B.ZIPCODE AS BZIP, 
S.LINE1, S.LINE2, S.CITY, S.STATE, S.ZIPCODE 
FROM CUSTOMERS C JOIN ADDRESSES S ON C.SHIPPINGADDRESSID = S.ADDRESSID, CUSTOMERS U JOIN ADDRESSES B ON U.BILLINGADDRESSID = B.ADDRESSID;

答案 1 :(得分:2)

结果视图与表一样,应具有唯一的列名。 你的代码:

CREATE VIEW CustomerAddress
AS
SELECT C.CustomerID, C.EmailAddress, C.LastName, C.FirstName, 
B.Line1, B.Line2, B.City, B.State, B.ZipCode, 
S.Line1, S.Line2, S.City, S.State, S.ZipCode 
FROM Customers C JOIN Addresses S ON C.ShippingAddressID = S.AddressID, 
Customers U JOIN Addresses B ON U.BillingAddressID = B.AddressID;

B. *和S. *中的名字已经相同。列名在目标视图中应该是唯一的。

您可以通过更改列名来使列名唯一:例如:

S.Line1 as SLine1, S.Line2 as SLine2, ...etc

答案 2 :(得分:0)

尝试在首次加入时删除',Customers U'。

而不是

JOIN地址S ON C.ShippingAddressID = S.AddressID,Customers U

尝试

JOIN地址S ON C.ShippingAddressID = S.AddressID

答案 3 :(得分:0)

你的加入对我来说看起来很时髦(因为我不认为它会起作用)。你不能像你一样混合连接和旧语法。

您的选择列表中有两列,从两个不同的表中,您将

需要别名。

CREATE VIEW CustomerAddress
AS
SELECT C.CustomerID
, 
C.EmailAddress
, C.LastName
, C.FirstName
, B.Line1  --DUP
, B.Line2  --DUP
, B.City   --DUP
, B.State  --DUP
, B.ZipCode --DUP
, S.Line1  --DUP
, S.Line2  --DUP
, S.City --DUP
, S.State  --DUP
, S.ZipCode --DUP

答案 4 :(得分:0)

您需要区分视图中的地址列(您已经包含两次客户,这将导致交叉连接):

CREATE VIEW CustomerAddress
AS
SELECT C.CustomerID, C.EmailAddress, C.LastName, C.FirstName, 
    B.Line1 BillingLine1, B.Line2 BillingLine2, B.City BillingCity, B.State BillingState, B.ZipCode BillingZipCode, 
    S.Line1 ShippingLine1, S.Line2 ShippingLine2, S.City ShippingCity, S.State ShippingState, S.ZipCode ShippingZipCode 
FROM Customers C 
JOIN Addresses S ON C.ShippingAddressID = S.AddressID, 
JOIN Addresses B ON C.BillingAddressID = B.AddressID;