2015-11-03 30 views
1

數據庫描述

我有三個表組成的簡單數據庫的其他客戶:客戶,產品和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" 

回答

2

這基本上是一個自連接,也可能是一個聚合。例如,下面的獲取已購買類似的產品作爲另一所有的客戶,同類產品的數量排序:

select cp.id_customer, cp2.id_customer, count(*) 
from customer_product cp join 
    customer_product cp2 
    on cp.id_product = cp2.id_product 
group by cp.id_customer, cp2.id_customer 
order by cp.id_customer, count(*) desc; 

可以在附加信息帶來諸如通過做額外加入的客戶名稱。

1

雖然我不完全確定我瞭解這些條件,但有三個基本步驟可以將這個問題組合成一個查詢(或不是)。

  1. 獲得了客戶購買
  2. 把那買了同樣的產品
  3. 我們會根據這些ID

因此,客戶的詳細信息1,你做的客戶ID的產品一個簡單的選擇:

SELECT id_product FROM customer_product WHERE id_customer = 1 

爲2,則可以使用IN聲明:

SELECT * FROM customer_product WHERE id_product IN 
    (SELECT id_product FROM customer_product WHERE id_customer = 1); 

對於3使用JOINGROUP BY組合來從customer表中的相關細節。

0

首先找到至少2個客戶購買的產品列表,其次找到使用連接表的客戶名稱,第三個選擇客戶名稱一次。這裏是查詢:

select distinct c.name from(select c.name, p.name,cp.id_customer, cp.id_product from customer_product cp join customer c on c.id=cp.id_customer join product p on p.id=cp.id_customer where cp.id_product in(select id_product, total from(select id_product,count(*) as total from customer_product group by id_product)p where total>=2)p1)p2) 
相關問題