CREATE DATABASE test;
use test;
CREATE TABLE order_details (
customer_id int,
pid int
-- PRIMARY KEY (customer_id, pid) -- 设置联合主键
);
INSERT INTO order_details (customer_id, pid)
VALUES (1, 101),
(1, 102),
(1, 103),
(2, 101),
(2, 101),
(3, 104),
(3, 105),
(3, 106),
(3, 107);
select * from order_details;
select customer_id,COUNT(DISTINCT pid) AS unique_products_purchased
from order_details
GROUP BY customer_id
HAVING COUNT(DISTINCT pid) > 3;