Hello all hope all is well. Im still new to mysql and im beginning to use JOIN functions and solving a problem which involves the combination of 3 Tables. Ive followed the Syntax i found

SELECT t1.col, t3.col FROM table1 join table2 ON table1.primarykey = table2.foreignkey join table3 ON table2.primarykey = table3.foreignkey

The question is:

"Find a name of product ordered and order date of all products ordered by a customer with customer code SAVEA and such that quantity of product is 10."

Here is my Solution

SELECT CUSTOMER.CUSTOMER_CODE, ORDERS.ORDER_DATE, ORDER_DETAIL.PRODUCT_NAME, ORDER_DETAIL.QUANTITY
FROM ORDERS
JOIN CUSTOMER ON CUSTOMER.CUSTOMER_CODE = ORDERS.CUSTOMER_CODE
JOIN ORDER_DETAIL ON ORDERS.ORDER_ID = ORDER_DETAIL.ORDER_ID
WHERE ORDER_DETAIL.QUANTITY = '10';

and here are the 3 tables i am using, labeled.

TABLE CUSTOMER

CREATE TABLE CUSTOMER
(
    CUSTOMER_CODE   VARCHAR(5)  NOT NULL,
    COMPANY_NAME    VARCHAR(40) NOT NULL,
    CONTACT_NAME    VARCHAR(30),
    CONTACT_TITLE   VARCHAR(30),
    ADDRESS         VARCHAR(60),
    CITY        VARCHAR(15),
    REGION      VARCHAR(15),
    POSTAL_CODE     VARCHAR(10),
    COUNTRY         VARCHAR(15),
    PHONE       VARCHAR(24),
    FAX         VARCHAR(24),
    CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUSTOMER_CODE)
);

TABLE ORDERS

CREATE TABLE ORDERS
(
    ORDER_ID        DECIMAL(9)  NOT NULL,
    CUSTOMER_CODE   VARCHAR(5)  NOT NULL,
    EMPLOYEE_ID     DECIMAL(9)  NOT NULL,
    ORDER_DATE      DATE        NOT NULL,
    REQUIRED_DATE   DATE,
    SHIPPED_DATE    DATE,
    SHIP_VIA        VARCHAR(40),
    FREIGHT         DECIMAL(10,2)           DEFAULT 0,
    SHIP_NAME       VARCHAR(40),
    SHIP_ADDRESS    VARCHAR(60),
    SHIP_CITY       VARCHAR(15),
    SHIP_REGION     VARCHAR(15),
    SHIP_POSTAL_CODE    VARCHAR(10),
    SHIP_COUNTRY    VARCHAR(15),
    CONSTRAINT PK_ORDERS PRIMARY KEY (ORDER_ID),
    CONSTRAINT FK_CUSTOMER_CODE FOREIGN KEY (CUSTOMER_CODE) REFERENCES CUSTOMER(CUSTOMER_CODE),  
    CONSTRAINT FK_EMPLOYEE_ID FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID),  
    CONSTRAINT FK_SHIP_VIA FOREIGN KEY (SHIP_VIA) REFERENCES SHIPPER(COMPANY_NAME)  
);

TABLE ORDER_DETAIL

CREATE TABLE ORDER_DETAIL
(
    ORDER_ID         DECIMAL(9) NOT NULL,
    PRODUCT_NAME     VARCHAR(40)    NOT NULL,
    UNIT_PRICE       DECIMAL(10,2)  NOT NULL    DEFAULT 0,
    QUANTITY         DECIMAL(9) NOT NULL    DEFAULT 1 ,
    DISCOUNT         DECIMAL(4,2)   NOT NULL    DEFAULT 0,
    CONSTRAINT PK_ORDER_DETAIL PRIMARY KEY (ORDER_ID, PRODUCT_NAME),
    CONSTRAINT FK_ORDER_ID FOREIGN KEY (ORDER_ID) REFERENCES ORDERS (ORDER_ID),
    CONSTRAINT FK_PRODUCT_NAME FOREIGN KEY (PRODUCT_NAME) REFERENCES PRODUCT (PRODUCT_NAME),
    CONSTRAINT CK_ORDER_DETAIL_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
    CONSTRAINT CK_ORDER_DETAIL_QUANTITY CHECK (QUANTITY > 0),
    CONSTRAINT CK_ORDER_DETAIL_DISCOUNT CHECK (DISCOUNT between 0 and 1)
);

The exact error i get is ERROR 1054(42522) Unknown column 'CUSTOMER_CODE' in 'field list'. I believe i am follwing the right Syntax so maybe its an issue with a comma or my order. if anyone could help it would be much appreciated

0 Answers