2012-05-19 58 views
0

我有兩個表:加入兩個MySQL表,但附加條件?

產品:

+-------------------------------------------------+ 
| id | name | category  | price | 
+-------------------------------------- ----------+ 
| 1 | item1 |  1   | 0.99 | 
| 2 | item2 |  2   | 1.99 | 
| 3 | item3 |  3   | 2.95 | 
| 4 | item4 |  4   | 2.50 | 
+-------------------------------------------------+ 

圖片:

+--------------------------------------------------+ 
| id | file_name | p_id | priority | 
+-------------------------------------- -----------+ 
| 1 |  image1  |  1 |  0  | 
| 2 |  image2  |  1 |  1  | 
| 3 |  image3  |  2 |  2  | 
| 4 |  image4  |  3 |  2  | 
| 5 |  image5  |  3 |  3  | 
| 11 |  image6  |  3 |  5  | 
| 16 |  image7  |  4 |  1  | 
| 19 |  image8  |  4 |  7  | 
+--------------------------------------------------+ 

我需要把所有的產品信息,以及圖像的文件名的產品。請注意,產品可以有多個圖像;我想要最低優先。另外,我只希望獲得某個類別產品的結果。

所以,說我需要在類{1,2,3}產品信息,然後在查詢運行後的結果應該返回:

+-----------------------------------------------------------------+ 
| id | name | category  | price | file_name | 
+-------------------------------------- ----------+---------------+ 
| 1 | item1 |  1   | 0.99 |  image1 | 
| 2 | item2 |  2   | 1.99 |  image3 | 
| 3 | item3 |  3   | 2.95 |  image4 | 
+-------------------------------------------------+---------------+ 

我曾嘗試寫了幾個不同的連接語句,但他們都沒有工作;這並不奇怪,因爲在SQL方面,我是一個總新手。

任何幫助將不勝感激!

回答

2

這就是答案:

select a.id, a.name, a.category, a.price, b.filename as file_name 
from products a left join (
    select i.p_id, i.filename from (select id, min(priority) as min_p 
    from images group by p_id) q 
    left join images i on q.id = i.id 
) b on a.id = b.p_id 
where a.category in (1, 2, 3); 

說明:

首先,你需要得到一組,其中對於每個具有最低優先級的產品,從此查詢中獲得:

select id, min(priority) as min_p from images group by p_id; 

結果將是:

+----+----------+ 
| id | lowest_p | 
+----+----------+ 
| 1 |  0 | 
| 2 |  2 | 
| 3 |  2 | 
| 4 |  1 | 
+----+----------+ 
4 rows in set (0.00 sec) 

下一步會得到一個外連接,在這種情況下,我會選擇(任意根據我的偏好),左聯接:

select i.p_id, i.filename from (select id, min(priority) as min_p 
from images group by p_id) q left join images i on q.id = i.id; 

此查詢產生你想要什麼簡稱:

+------+----------+ 
| p_id | filename | 
+------+----------+ 
| 1 | image1 | 
| 2 | image3 | 
| 3 | image4 | 
| 4 | image7 | 
+------+----------+ 
4 rows in set (0.00 sec) 

現在你只需要裝飾這一點,再次使用左連接:

select a.id, a.name, a.category, a.price, b.filename as file_name 
from products a left join (
    select i.p_id, i.filename from (select id, min(priority) as min_p 
    from images group by p_id) q 
    left join images i on q.id = i.id 
) b on a.id = b.p_id 
where a.category in (1, 2, 3); 

,你會得到你想要的東西:

+------+-------+----------+-------+-----------+ 
| id | name | category | price | file_name | 
+------+-------+----------+-------+-----------+ 
| 1 | item1 |  1 | 0.99 | image1 | 
| 2 | item2 |  2 | 1.99 | image3 | 
| 3 | item3 |  3 | 2.95 | image4 | 
+------+-------+----------+-------+-----------+ 
3 rows in set (0.00 sec) 

你也可以把產品在左邊的右邊加入,這取決於你所期望的時候有沒有圖像產品可用。上面的查詢將顯示上面的視圖,其中file_name字段爲「null」。

另一方面,如果您將產品放在左側連接的右側,它將不會顯示任何內容。

4

我將添加一步一步的教程,首先獲取聯合權利, 然後添加一些條件來過濾類別,最後,分組 和使用具有子選擇的having子句。您需要在代碼中使用最後選擇的 。我也在一個mysql實例上測試了它,它工作。 如果您需要其他複雜的東西,我正在使用group。有一個例子是很好的。 語法ANSII SQL,就應該對所有數據庫的工作不只是在mysql

-- get everything by joining 
select p.*, i.file_name 
from products p 
join image i on (p.id = i.p_id) 



/* get everything by joining 
* + filter by category 
*/ 
select p.*, i.file_name 
from products p 
join image i on (p.id = i.p_id) 
where p.category in (1,2,3) 


/* get everything by joining 
* + filter by category 
* + image is the one with the lowest priority 
* note: selecting the priority is not necessary 
* but it's good for demonstration purposes 
*/ 
select p.*, i.file_name, i.priority 
from products p 
join image i on (p.id = i.p_id) 
where p.category in (1,2,3) 
group by p.id 
having i.priority = (select min(priority) from image where p_id = p.id) 
0

大廈sarwar026的答案...

SELECT p.id, name, priority, price, file_name 
FROM Products p, Images i 
WHERE p.id = i.p_id 
    AND i.priority = (SELECT MIN(priority) FROM Images ii WHERE ii.p_id = p.id) 
    AND p.category IN (1,2,3) 

(與你的表的副本MySQL數據庫上測試)