2012-10-03 77 views
-4

表結構....這裏只有3個表..(買了,客戶,產品)。 ..內部加入多對多表格與MySQL中的過濾器

bought table structure : fields (id,product_id,customer_id), 
customer table structure: (customer_id,name,address) , 
product table structure: (product_id,name). 

產品表內包含產品a和b。從購買的表格中,我想獲得客戶ID(購買產品'a'但未購買產品'b'的客戶)。

使用mysql或類似的,你會如何選擇所有購買產品'a'但沒有購買產品'b'的客戶,而不使用子選擇。 (確保沒有客戶購買產品'b'的結果)。..請幫助我

+0

您只需編輯您剛纔的問題..........但你不說什麼你已經嘗試了? – user786

+0

你在你的問題中說過,「沒有顧客在結果中買過產品'b',這是什麼意思?如果我正確地理解沒有顧客購買產品'b',那麼你想要的只是購買產品'a'的客戶,這可以簡單地通過使用select查詢來完成...... – Shubhansh

+0

由於涉及3個表,因此可能無法使用簡單的SELECT查詢來完成。 。如果你不能解決MySQL的過濾問題,可以通過過濾數據來解決它。 –

回答

0

林不知道,你想按產品名稱過濾?也許我失去了你的問題的東西,但我認爲這應該工作

select customer_id 
from 
bought b 
inner join customer c on b.customer_id = b.customer_id 
inner join product p on p.product_id = b.product_id 
where p.name = 'a' 
+0

當前查詢顯示一個c ommon產品命名爲1 ..但僅顯示產品..... – sanal

+0

這怎麼會發生?在哪裏聲明'=',名爲'1'的產品不會顯示 – brazorf

0

我想誰買產品「A」,但沒買 產品「B」

客戶

試試這個:

SELECT * 
FROM Customers c 
INNER JOIN 
(
    SELECT * 
    FROM bought 
    WHERE product_id = id of 'a' 
) ba ON c.CustomerId = ba.CustomerId 
LEFT JOIN 
(
    SELECT * 
    FROM bought 
    WHERE product_id = id of 'b' 
) bb ON c.CustomerId = bb.CustomerId 
WHERE bb.CustomerId IS NULL; 
+0

嗨,,必須說,沒有使用子選擇.. – sanal

+0

@sanal請參閱我的編輯 –

+0

感謝所有..我希望使用子查詢查詢, ..「現在我得到了答案..」「選擇b.customer_id從購買b內部加入客戶c上c.customer_id = b.customer_id內部加入產品p上p.product_id = b.product_id其中p.name ='a'和b.customer_id NOT IN(p.name ='a')「」..謝謝 – sanal

0

下面是整個表和連接的答案。

客戶

CREATE TABLE customer (
    customer_id int NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    name varchar(50), 
    address varchar(100) 
) ENGINE=InnoDB; 

產品

CREATE TABLE product (
    product_id int NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    name varchar(50) 
) ENGINE = InnoDB; 

購買或交易

CREATE TABLE bought (
    id int NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    product_id int, 
    customer_id int, 
    FOREIGN KEY (product_id) REFERENCES product(product_id), 
    FOREIGN KEY (customer_id) REFERENCES customer(customer_id) 
) ENGINE=InnoDB; 

插入值

INSERT INTO customer (name, address) VALUEs ("George","Kentucky") 
INSERT INTO customer (name, address) VALUEs ("Stan","Dublin") 
INSERT INTO customer (name, address) VALUEs ("Kazaam","UAE") 
INSERT INTO customer (name, address) VALUEs ("Sarah","New York") 

INSERT INTO product (name) VALUES ("tomato") 
INSERT INTO product (name) VALUES ("apple") 

INSERT INTO bought (customer_id, product_id) VALUEs("1","2") 
INSERT INTO bought (customer_id, product_id) VALUEs("2","1") 
INSERT INTO bought (customer_id, product_id) VALUEs("3","1") 
INSERT INTO bought (customer_id, product_id) VALUEs("4","1") 

內與過濾加入 - >這將是你的$查詢

SELECT customer.customer_id, customer.name as customer_name, product.name as product_name 
FROM customer 
INNER JOIN bought ON customer.customer_id = bought.customer_id 
INNER JOIN product ON product.product_id = bought.product_id 
WHERE product.name LIKE "apple" AND product.name NOT LIKE "tomato" 
ORDER BY customer.name 

這將過濾所有誰買蘋果的客戶。您只需更改product.name的值即可。如果要更改字段,請更改INNER JOIN的第一行。

維恩圖解釋: 如果你打算考慮一下,MySQL會遵循維恩圖的設計。它不能相交3個對象並對其進行過濾。你試圖通過在中間構造另一個圓來超越維恩圖的設計。見下圖。

enter image description here

所以,來解決PHP您的問題,我們創建了內圓。您運行一個循環以獲取product_name上所有具有「蘋果」的customer_id(s)。然後再次執行mysql_fetch數組的循環。並只輸出帶有「蘋果」的customer_id。「

如果不能由MySQL解決過濾,通過過濾的數據與PHP解決它。