选择过滤条件及其计数

时间:2012-12-31 04:32:29

标签: mysql sql select

我有两张桌子:

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

1 个答案:

答案 0 :(得分:0)

尝试一下,

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