數據庫描述
我有三個表組成的簡單數據庫的其他客戶:客戶,產品和custumer_product。爲每一位客戶都選擇購買了相同的項目
- 客戶:包含有關客戶的信息。他的身份證和姓名
- 產品:包含有關商店中可用產品的信息。 ID和姓名
- custumer_product:結表
- customer (table)
id integer primary key not null
name TEXT
- custumer_product (table)
id_product integer
id_customer integer
primary key(id_product, id_customer)
FOREIGN KEY(Id_product) REFERENCES product(id)
FOREIGN KEY (ID_customer) REFERENCES customer(ID)
- product (table)
id integer primary key not null
name TEXT
三個表已在sqlfiddle使用SQLITE
初始化。下面的SQL查詢來構建數據庫
create table if not exists customer (id integer primary key not null, name TEXT);
create table if not exists product (id integer primary key not null, name TEXT);
create table if not exists customer_product (id_product integer, id_customer
integer, primary key(id_product, id_customer), FOREIGN KEY(Id_product) REFERENCES product(id), FOREIGN KEY (ID_customer) REFERENCES customer(ID));
insert into customer(id,name) values(1,"john");
insert into customer(id,name) values(2,"Paul");
insert into customer(id,name) values(3,"Jenny");
insert into customer(id,name) values(4,"Fred");
insert into customer(id,name) values(5,"Lea");
insert into product(id,name) values(1,"Mouse");
insert into product(id,name) values(2,"screen");
insert into product(id,name) values(3,"pc");
insert into product(id,name) values(4,"CD");
insert into product(id,name) values(5,"Game");
insert into customer_product values(1,1);
insert into customer_product values(1,2);
insert into customer_product values(1,3);
insert into customer_product values(2,1);
insert into customer_product values(2,2);
insert into customer_product values(2,3);
insert into customer_product values(3,4);
insert into customer_product values(4,5);
insert into customer_product values(5,5);
問題
爲每一位客戶我想選擇所有買至少一個類似的產品的其他顧客。
- 約翰和保羅買了至少1同類產品
- 沒有客戶購買類似的產品珍妮又
- 弗雷德和LEA買了一個類似的產品
輸出
"John" "Paul"
"Jenny"
"Fred" "Lea"