列出已完成2个以上订单的所有客户ID和姓氏

时间:2015-11-26 11:50:01

标签: mysql sql-server

我需要以下代码 客户表

CREATE TABLE customer (
  customer_id INTEGER PRIMARY KEY,
  first_name VARCHAR(255) NOT NULL,
  last_name VARCHAR(255),
  city VARCHAR(50) NOT NULL,
  State_ VARCHAR(50) NOT NULL,
  zip VARCHAR(7) NOT NULL
)

订购表

CREATE TABLE order_(
order_number INTEGER PRIMARY KEY NOT NULL,
Customer_ID integer FOREIGN KEY REFERENCES customer (customer_id) Not NULL,
Date_ date NOT NULL,
)

如何显示订单超过2个的客户ID和姓氏 请帮助

2 个答案:

答案 0 :(得分:1)

尝试此查询

Select customer_id,last_name From  Customer
Where customer_id in 
(Select customer_id From Order group by customer_id having count(customer_id) > 2 )

答案 1 :(得分:0)

SELECT customer_id, last_name
FROM dbo.customer
WHERE customer_id IN (
    SELECT Customer_ID
    FROM order_
    GROUP BY Customer_ID
    HAVING COUNT(*) > 2
)