简体   繁体   中英

Ambiguous column name 'ProductNumber'

Can't figure out why my question 3 is getting the error mentioned above.

Below is my code

/* Question 1 */
CREATE TABLE CUSTOMERS 
( 
    CustomerID INT PRIMARY KEY, 
    CustFirstName VARCHAR(50) NOT NULL, 
    CustLastName VARCHAR(50) NOT NULL, 
    CustStreetAddress VARCHAR(50) NOT NULL, 
    CustCity VARCHAR(50) NOT NULL, 
    CustState VARCHAR(26) NOT NULL, 
    CustZipCode INT NOT NULL, 
    CustAreaCode INT NOT NULL, 
    CustPhoneNumber VARCHAR (26) NOT NULL
);  

CREATE TABLE EMPLOYEES 
( 
    EmployeeID INT PRIMARY KEY, 
    EmpFirstName VARCHAR(50) NOT NULL, 
    EmpLastName VARCHAR(50) NOT NULL, 
    EmpCity VARCHAR (50) NOT NULL, 
    EmpState VARCHAR(26) NOT NULL, 
    EmpZipCode INT NOT NULL, 
    EmpAreaCode INT NOT NULL, 
    EmpPhoneNumber VARCHAR(26) NOT NULL, 
    EmpBirthDate DATE NOT NULL
); 

CREATE TABLE ORDERS
( 
    OrderNumber INT PRIMARY KEY, 
    OrderDate DATE NOT NULL, 
    ShipDate DATE NOT NULL, 
    CustomerID INT NOT NULL, 
    EmployeeID INT NOT NULL, 

    FOREIGN KEY(EmployeeID) REFERENCES EMPLOYEES(EmployeeID), 
    FOREIGN KEY(CustomerID) REFERENCES CUSTOMERS(CustomerID)
); 

CREATE TABLE CATEGORIES
( 
    CategoryID INT PRIMARY KEY, 
    CategoryDescription VARCHAR(255) NOT NULL
); 

CREATE TABLE PRODUCTS
(
    ProductNumber INT PRIMARY KEY, 
    ProductName VARCHAR(50) NOT NULL, 
    ProductDescription VARCHAR(255) NOT NULL,
    RetailPrice INT NOT NULL, 
    QuantityOnHand INT NOT NULL, 
    CategoryID INT NOT NULL, 

    FOREIGN KEY(CategoryID) REFERENCES CATEGORIES (CategoryID)
);    

CREATE TABLE ORDER_DETAILS
( 
    OrderNumber INT NOT NULL, 
    ProductNumber INT NOT NULL, 
    QuotedPrice INT NOT NULL, 
    QuantityOrdered INT NOT NULL, 

    PRIMARY KEY (OrderNumber, ProductNumber), 
    FOREIGN KEY (OrderNumber) REFERENCES ORDERS(OrderNumber), 
    FOREIGN KEY(ProductNumber) REFERENCES PRODUCTS(ProductNumber)
);   

CREATE TABLE VENDORS
(
    VendorID INT PRIMARY KEY, 
    VendName VARCHAR(100) NOT NULL, 
    VendStreetAddress VARCHAR(50) NOT NULL, 
    VendCity VARCHAR(50) NOT NULL, 
    VendState VARCHAR(26) NOT NULL, 
    VendFaxNumber VARCHAR(50) NOT NULL, 
    VendWebPage VARCHAR(100) NOT NULL, 
    VendEmailAddress VARCHAR(100) NOT NULL
); 

CREATE TABLE PRODUCT_VENDORS
(
    ProductNumber INT NOT NULL, 
    VendorID INT NOT NULL, 
    WholeSalePrice INT NOT NULL,
    DaysToDeliver INT NOT NULL, 

    PRIMARY KEY(ProductNumber, VendorID), 
    FOREIGN KEY(ProductNumber) REFERENCES PRODUCTS(ProductNumber), 
    FOREIGN KEY(VendorID) REFERENCES Vendors(VendorID)
); 

/* QUESTION 2 */

SELECT 
    OrderDate, CustFirstName, CustLastName
FROM 
    CUSTOMERS C, ORDERS O
WHERE 
    C.CustomerID = O.OrderNumber; 

/* QUESTION 3 */

SELECT 
    ProductNumber, WholeSalePrice, VendName 
FROM 
    PRODUCTS P, PRODUCT_VENDORS PV, VENDORS V 
WHERE 
    P.PRODUCTNUMBER = PV.ProductNumber AND PV.VendorID = V.VendorID; 

I got the error

Ambiguous column name 'ProductNumber'

Because there are two table have column name of ProductNumber in your query you need to tell DB engine which column you want to get.

Also, use JOIN instead of , comma CROSS JOIN , because Join is more clear than a comma on the relationship between the two tables

SELECT PV.ProductNumber, WholeSalePrice, VendName 
FROM PRODUCTS P 
JOIN PRODUCT_VENDORS PV ON P.PRODUCTNUMBER = PV.ProductNumber
JOIN VENDORS V ON PV.VendorID = V.VendorID; 

Your query stipulates SELECT ProductNumber From two tables that both have ProductNumber. In the SELECT columns list, prefix ProductNumber with the table name from which you want to receive the data.

also, Have a look at your question 2. I don't think you really want to join customer id to order number, and you should use JOIN syntax intead of joining in the WHERE clause

Ambiguous error means that you are calling a certain field in which exist in both Table and the SQL has no idea where to get it. See, with your query, you're just literally calling the ProductNumber field in which the SQL has no idea where to get it since there are ProductNumber fields on both Tables, and you didn't specify any table.

so it is better when you have same column in multiple tables use table preface 1st then column like table1.col1,table2.col2 so in your case

 SELECT p.ProductNumber, WholeSalePrice, v.VendName     
   FROM PRODUCTS P join 
  PRODUCT_VENDORS PV  on P.PRODUCTNUMBER = PV.ProductNumber //
  join   VENDORS V on PV.VendorID = V.VendorID //use join instead your's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM