2016-01-14 58 views
1

我有三個表(product,product_description,product_store_description)。即使連接數據爲空,Mysql如何返回行

產品

此表有一行

+----------+---------------------+ 
|product_id|  description | 
+----------+---------------------+ 
| 1  |'regular description'| 
+----------+---------------------+ 

PRODUCT_DESCRIPTION

此表的行數爲零

+----------+-----------+--------+ 
|product_id|description|language| 
+----------+-----------+--------+ 

product_store_description

This table has zero rows 

+----------+-----------+--------+--------+ 
|product_id|description|language|store_id| 
+----------+-----------+--------+--------+ 

我想對所有表WHERE product.product_id = '1'返回.description列。

因此,在這種情況下,我想輸出將

$row = $result->fetch_assoc(); 
echo '<pre>'; 
var_dump($row); 
echo '</pre>'; 

array (size=3) 
    'p_description' => string 'regular description' (length=19) 
    'pd_description' => string '' (length=0) 
    'psd_description' => string '' (length=0) 

我已經盡了SQL的許多變化,這是它如何矗立在那一刻。

SELECT p.description AS p_description ,pd.description AS pd_description, psd.description AS psd_description FROM product AS p 
RIGHT JOIN product_description AS pd ON(pd.product_id = p.product_id) 
RIGHT JOIN product_store_description AS psd ON(psd.product_id = p.product_id) 
WHERE p.product_id = '1' 
AND pd.language = 'en' 
AND psd.language = 'en' AND psd.store_id = 1; 

這將返回零行

...我已經尋找這導致我使用RIGHT JOIN但這並沒有解決我的問題的答案。

我創建了示例表的SQL小提琴HERE,不幸的是我還沒有能夠得到任何人的工作答案。

+0

說明字段必須具有相同的值嗎? (表的模式非常糟糕和冗餘) – MTroy

+0

@MTroy:看起來像第二個表中的描述是依賴於語言的,在第三個表中它可以根據商店進一步進行本地化。如果不理解應用程序,您不應該對設計做出價值判斷。 – PaulF

回答

2

如果表product是包含ATLEAST一個數據。並且您要顯示其他列NULL或自定義字符串,如"---",即使連接屬性爲null,您也必須在查詢中指定該連接。所以下面的查詢會給你數據。

SELECT 
p.description AS p_description ,ifnull(pd.description,"---") AS pd_description, 
ifnull(psd.description,"--") AS psd_description 
FROM product AS p 
LEFT JOIN Product_description AS pd ON(pd.product_id = p.product_id) 
LEFT JOIN product_store_description AS psd ON(psd.product_id = p.product_id) 
WHERE p.product_id = '1' AND (pd.language = 'en' or pd.language is null) 
AND (psd.language = 'en' or psd.language is null) AND 
(psd.store_id = 1 or psd.store_id is null); 
+1

太棒了!這工作完美。解決方案並不像我想的那麼簡單,謝謝。 – TarranJones

0

如果我理解你是正確的,那麼你需要LEFT join,因爲產品表位於查詢中連接的左側。

左連接將返回連接運算符左側表中的所有行,而右連接將返回連接運算符右側表中的所有行。

+0

@Shadow:感謝編輯 - 在添加細節之前,我被同事分散注意力。 – PaulF

+0

謝謝,但我用左連接開始,這返回0行。 – TarranJones

+0

嘗試將第2個/第3個表條件移動到連接:_SELECT p.description AS p_description,pd.description AS pd_description,psd.description AS psd_description FROM product AS p LEFTJOIN product_description AS pd ON(pd.product_id = p.product_id )AND pd.language ='en' LEFT JOIN product_store_description AS psd ON(psd.product_id = p.product_id) AND psd.language ='en'AND psd.store_id = 1 WHERE p.product_id ='1'; _ – PaulF

-1

如果你想獲得一些數據,你應該這樣執行查詢:

SELECT 
    p.description AS p_description , 
    pd.description AS pd_description, 
    psd.description AS psd_description, 
    pd.language as language, 
    psd.store_id as store_id 
FROM 
    product AS p LEFT JOIN product_description AS pd ON(pd.product_id = p.product_id) 
    LEFT JOIN product_store_description AS psd ON(psd.product_id = p.product_id) 
WHERE 
    p.product_id = '1' 

但在這種情況下,您手動必須控制語言值和STORE_ID

+0

這將返回任何商店或任何語言的描述 – TarranJones

+0

@TarranJones這就是爲什麼我寫道「但在這種情況下,你手動不得不控制值的語言和store_id」 – KLin