我有两张桌子:
表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
答案 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