在sql中使用子查询。不显示输出

时间:2018-06-10 21:30:43

标签: sql subquery

我的任务是检索订单尚未发货的客户的名字和姓氏。以下是获得此结果的表格↓↓↓

CREATE TABLE employees
(
    employee_id INT NOT NULL,
    last_name VARCHAR(20) NOT NULL,
    first_name VARCHAR(20) NOT NULL,
    manager_id INT,

    CONSTRAINT employees_pk PRIMARY KEY (employee_id),
    CONSTRAINT emp_fk_mgr 
        FOREIGN KEY (manager_id) REFERENCES employees(employee_id) 
);

CREATE TABLE orders
(
    order_id INT NOT NULL,
    customer_id INT NOT NULL,
    order_date DATE NOT NULL,
    shipped_date DATE,
    employee_id INT,

    CONSTRAINT orders_pk PRIMARY KEY (order_id),
    CONSTRAINT orders_fk_customers
        FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
    CONSTRAINT orders_fk_employees
        FOREIGN KEY (employee_id) REFERENCES employees (employee_id)
);

CREATE TABLE order_details
(
    order_id INT NOT NULL,
    item_id INT NOT NULL,
    order_qty INT NOT NULL,

    CONSTRAINT order_details_pk PRIMARY KEY (order_id, item_id),
    CONSTRAINT order_details_fk_orders
        FOREIGN KEY (order_id) REFERENCES orders (order_id),
    CONSTRAINT order_details_fk_items
        FOREIGN KEY (item_id) REFERENCES items (item_id)
);

目前,我有以下查询,但它一直生成无输出。

SELECT c.customer_first_name
FROM order_details od
JOIN orders o ON od.order_id = o.order_id
JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id IN (SELECT shipped_date FROM orders WHERE shipped_date IS NULL);

有人可以帮助我获得我需要的结果吗?

1 个答案:

答案 0 :(得分:2)

据推测,您的客户不是日期,您实际上打算:

WHERE c.customer_id IN (SELECT o.customer_id FROM orders o WHERE shipped_date IS NULL)