Mysql | Faceted search

邮差的信 提交于 2019-12-06 22:57:26

Instead of using a subquery, you may get the values from a left join and count the distinct not null values.

SELECT meta_name, meta_value, COUNT(DISTINCT pid) count
  FROM meta m JOIN item_meta im
    ON im.field_id = m.id
LEFT JOIN (
    SELECT i.id pid
  FROM item_meta im JOIN items i
    ON im.item_id = i.id JOIN meta m
    ON im.field_id = m.id
 GROUP BY i.id
HAVING MAX(meta_name = 'Make' AND meta_value = 'BMW') = 1
   AND MAX(meta_name = 'Car Type' AND meta_value = 'Coupe') = 1)
LJ ON im.item_id = LJ.pid
 GROUP BY meta_name, meta_value;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!