我有一個cs cart數據庫,我試圖選擇所有產品的所有屬性,問題是對於產品的每個單獨屬性,我的查詢創建一個新的行,我想爲每個產品擁有一行,並將所有屬性分爲列。SQL:將具有相同ID的行拆分爲列+左連接
這是我的查詢現在:
SELECT a.product_id, b.variant, c.description, d.product_code
FROM cscart_product_features_values a
LEFT JOIN cscart_product_feature_variant_descriptions b ON a.variant_id = b.variant_id
LEFT JOIN cscart_product_features_descriptions c ON a.feature_id = c.feature_id
LEFT JOIN cscart_products d ON a.product_id = d.product_id
我運行查詢後,我得到以下結果:
product_id;"variant";"description";"product_code"
38;"1st";"Grade Level";"750"
38;"Math";"Subject Area";"750"
38;"Evan-Moor";"Publisher";"750"
etc next product
我想是這樣的:
product_id;"product_code";"Grade Level";"Subject Area";"Publisher"
38;"750";"1st";"Math";"Evan-Moor"
etc next product
我們只有3種屬性:成績等級,學科領域和出版商。
任何想法如何提高我的查詢和實現?即使連接了由「,」分隔的一列中的所有3個屬性,我也會很高興。
搜索'PIVOT' –