2013-05-10 81 views
-1

我想查詢我的opencart數據庫,並且想要獲取與某個產品相關的所有屬性(如果可能的話)。SQL幫助 - 每個產品需要一行,每個屬性不需要一行?

這是我到目前爲止。它爲每個產品提供所有屬性,但是爲每個屬性創建一個全新的行。如果可能,我需要一行中的所有屬性。

謝謝!

SELECT 
    product.product_id, 
    product.model, 
    product_description.name, 
    attribute_description.name, 
    text 
FROM 
    product, 
    product_attribute, 
    attribute, 
    attribute_description, 
    product_description 
WHERE 
    product.product_id = product_attribute.product_id AND 
    product_attribute.attribute_id = attribute.attribute_id AND 
    attribute_description.attribute_id = attribute.attribute_id AND 
    product.product_id = product_description.product_id 
order by 
    product_id, attribute_description.name; 

下面是更新後的代碼:

SELECT 
    p.product_id, 
    p.model, 
    pd.name, 
    GROUP_CONCAT(ad.name ORDER BY ad.name SEPARATOR ',') AS attributes_group, 
    GROUP_CONCAT(pa.text ORDER BY ad.name SEPARATOR ',') AS attributes 
FROM product p 
    LEFT JOIN product_attribute pa ON p.product_id = pa.product_id 
    LEFT JOIN attribute_description ad ON pa.attribute_id = ad.attribute_id 
    LEFT JOIN product_description pd ON p.product_id = pd.product_id 
GROUP BY 
    p.product_id, p.model, pd.name 
ORDER BY 
    p.product_id; 

回答

0

你想要做的是聚集在一起。你應該學習正確的join語法,但我不打算在這裏解決(將連接條件放在on子句中,而不是where子句中)。

下面是MySQL數據庫語法,做你想做什麼:

SELECT 
    product.product_id, 
    product.model, 
    product_description.name, 
    group_concat(attribute_description.name) as attributes, 
    text 
FROM 
    product, 
    product_attribute, 
    attribute, 
    attribute_description, 
    product_description 
WHERE 
    product.product_id = product_attribute.product_id AND 
    product_attribute.attribute_id = attribute.attribute_id AND 
    attribute_description.attribute_id = attribute.attribute_id AND 
    product.product_id = product_description.product_id 
group by 
    product.product_id, 
    product.model, 
    product_description.name; 

如果不使用MySQL,大多數其他數據庫具有用聚合串聯字符串值類似的功能。在您的查詢

+0

這給了我,我是用代碼得到同樣的結果我已經發布。 – 2013-05-11 04:57:14

+0

@BobbyLipps。 。 。在這種情況下,'text'可能來自屬性表。鑑於我使用MySQL語法,我只是從'group by'子句中刪除它。在其他數據庫中,您需要使用'min()'或'max()'。 – 2013-05-11 15:49:02

+0

你好,我刪除了該組,並且它幫助了一些我想。它把所有的屬性名稱放在一個表格單元格中,我不能說,但我認爲它對文本列做了同樣的事情。最後,它只返回一列。 – 2013-05-13 12:47:12

0

的樣子,我知道你可能從未聽說過的SQL查詢JOIN ...

因此,這裏是你的查詢中使用JOIN小號改寫:

SELECT 
    p.product_id, 
    p.model, 
    pd.name, 
    ad.name, 
    pd.text /* <- don't know where the column is coming from, probably something custom */ 
FROM product p 
    LEFT JOIN product_attribute pa ON p.product_id = pa.product_id 
    LEFT JOIN attribute_description ad ON pa.attribute_id = ad.attribute_id 
    LEFT JOIN product_description pd ON p.product_id = pd.product_id 
PRDER BY 
    p.product_id, ad.name 

現在,使用JOIN小號,這個查詢會更快(取決於總行數)。

你的問題:可以使用GROUP_CONCAT()函數加載的所有屬性爲一行:

SELECT 
    p.product_id, 
    p.model, 
    pd.name AS product_name, 
    GROUP_CONCAT(ad.name ORDER BY ad.name SEPARATOR ',') AS attributes, 
    pd.text /* <- don't know where the column is coming from, probably something custom */ 
FROM product p 
    LEFT JOIN product_attribute pa ON p.product_id = pa.product_id 
    LEFT JOIN attribute_description ad ON pa.attribute_id = ad.attribute_id 
    LEFT JOIN product_description pd ON p.product_id = pd.product_id 
GROUP BY 
    p.product_id, p.model, pd.name, pd.text 
ORDER BY 
    p.product_id 

PHP然後,您可以只回聲「$結果[」屬性] or爆炸it to get array of the attributes $ attributes = explode(',',$ result ['attributes']);`。總之,我想你是聰明的,你是剛裝的屬性名稱,而不是實際的屬性值對的事實...


編輯:

可以導出爲CSV只一個普通的結構(每行一個入口) - 正是你要求的。平原結構意味着像product + product_descriptioncategory + category_description。如果您添加attribute + attribute_value + attribute_description表格,則結構不再簡單。對於這種情況,導出爲XML會更好。

則結構看起來是這樣的:

<?xml ... ?> 
<products> 
    <product> 
     <title></title> 
     <description></description> 
     ... 
     <attributes> 
      <attribute> 
       <title></title> 
       <description></description> 
       <value></value> 
      </attribute> 
      <attribute> 
       <title></title> 
       <description></description> 
       <value></value> 
      </attribute> 
      ... 
     </attributes> 
    </product> 
    ... 
</products> 

但還是,也許你可以做到這一點使用一個SQL和導出爲CSV,但你必須在屬性名逗號連接在一起一列和屬性值逗號連接起來的另一列...用於自動導入,也不是由進口phpMyAdmin的......反正沒事,你可以試試這個:

SELECT 
    p.product_id, 
    p.model, 
    pd.name, 
    GROUP_CONCAT(ad.name ORDER BY ad.name SEPARATOR ',') AS attributes, 
    GROUP_CONCAT(pa.text ORDER BY ad.name SEPARATOR ',') AS values /* <- problem could be with values ordering */ 
FROM product p 
    LEFT JOIN product_attribute pa ON p.product_id = pa.product_id 
    LEFT JOIN attribute_description ad ON pa.attribute_id = ad.attribute_id 
    LEFT JOIN product_description pd ON p.product_id = pd.product_id 
GROUP BY 
    p.product_id, p.model, pd.name 
ORDER BY 
    p.product_id 
+0

其實我想要屬性值。簡而言之,我想獲取屬性組名稱並將這些列標題添加到列下的值中。上面的代碼幾乎給了我原來的代碼。 – 2013-05-13 12:16:48

+0

然後,所需的結果不能通過**一個單獨的SQL查詢來實現** ...您必須單獨執行 - 在一個查詢中加載產品數據,然後爲具體產品加載另一個查詢中的屬性。 「文本」列來自哪裏?它包含什麼值? – shadyyx 2013-05-13 12:26:48

+0

「文本」來自product_attribute表。我包含的產品屬性(明顯)從HTML代碼鏈接到PDF的高度重量顏色等。 – 2013-05-13 12:43:22