2011-10-23 71 views
0

我有一個屬性系統,我可以在其中將自定義字段應用於產品,如品牌,尺寸等...... 在網站的前端,您會看到一個列表產品和屬性要過濾的名單和項目的數量預期:屬性查詢;還包括內部連接沒有匹配的屬性

  • 品牌A(12)
  • 品牌B(8)
  • ...

尺寸:

  • 大小(9)
  • 尺寸B(11)

我想更新的屬性列表,並過濾後包括那些沒有「命中」的記載。用我目前的查詢,我只會得到那些有記錄的記錄。

總之:如何獲得0項目屬性?

我有此查詢(例如一個屬性過濾器中選擇)

SELECT 
    attr.id, attr.title 
    , attrval.value, attrval.id as valid 
    , COUNT(attrlink.id) AS attrcount 
FROM #__foc_users_attributes AS attr 
LEFT JOIN #__foc_users_attr_val AS attrval 
     ON attrval.attrid = attr.id AND attrval.value !='' 
LEFT JOIN #__foc_users_attr_link AS attrlink 
     ON (attrlink.valueid = attrval.id) 
LEFT JOIN #__foc_users_items AS item 
     ON (item.id = attrlink.itemid) 
INNER JOIN #__foc_users_attr_link AS attrlink1 
     ON attrlink1.itemid = item.id 
     AND ((attrlink1.attrid=1 AND attrlink1.valueid=33)) 
WHERE item.published=1 AND attr.published = 1 AND attrval.value != '' 
GROUP BY attrval.id 
+0

也許表架構可以提供幫助。解決方案可能會將#__foc_users_items寫入第一個表,併爲其他所有人進行左連接,爲什麼#__foc_users_attr_link有內連接而不是左連接? – danihp

+0

你是對的;我爲每個不同的屬性組使用了左連接,並立即開始工作!感謝您的回覆。 – Bert

回答

1
SELECT 
    attr.id, attr.title 
    , attrval.value, attrval.id as valid 
    , COUNT(attrlink.id) AS attrcount 
FROM #__foc_users_attributes AS attr 
LEFT JOIN #__foc_users_attr_val AS attrval 
     ON attrval.attrid = attr.id AND attrval.value !='' 
LEFT JOIN #__foc_users_attr_link AS attrlink 
     ON (attrlink.valueid = attrval.id) 
LEFT JOIN #__foc_users_items AS item 
     ON (item.id = attrlink.itemid) 
LEFT JOIN #__foc_users_attr_link AS attrlink1 <<-- replace inner with LEFT join 
     ON attrlink1.itemid = item.id 
     AND ((attrlink1.attrid=1 AND attrlink1.valueid=33)) 
WHERE item.published=1 AND attr.published = 1 AND attrval.value != '' 
GROUP BY attrval.id