属性查询;还包括内部联接没有命中的属性

时间:2011-10-23 13:40:10

标签: mysql attributes

我有一个属性系统,我可以在其中将自定义字段应用于产品,如品牌,尺寸等...... 在网站的前端,您将看到产品列表和要过滤的属性列表以及预期的项目数:

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

尺寸:

  • 尺寸a(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

1 个答案:

答案 0 :(得分: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