1

我有两张桌子:

桌子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
4

1 回答 1

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
于 2012-12-31T04:37:33.287 回答