我有两个表:

Filters

id   INT(11) PRIMARY KEY
text VARCHAR(50)


items

id    INT(11) PRIMARY KEY
title VARCHAR(255)


我想将所有过滤器术语以及它们在标题中的出现数一起计数

我目前正在使用此语句,但计数为零

SELECT  filters.text,
        (SELECT COUNT(items.id)
         FROM items
         WHERE (items.title LIKE 'filters.text' OR
                items.title LIKE '%filters.text' OR
                items.title LIKE '%filters.text%' OR
                items.title LIKE 'filters.text%')
        ) AS count
        FROM filters, items
GROUP BY filters.ID
ORDER BY filters.ID DESC

最佳答案

试试看

SELECT  a.text, COUNT(b.id) totalCount
FROM    filters a
        LEFT JOIN items b
            ON b.title LIKE CONCAT('%',a.text,'%')
GROUP BY a.text
ORDER BY totalCount DESC



SQLFiddle Demo Link

关于mysql - 选择过滤条件及其计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14097470/

10-13 00:59