2012-12-31 33 views
1

我有两个表:过滤条件

Filters

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

items

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

我想dipslay所有过滤条件与计数一起FO他们之间的标题发生

我curre ntly使用这种说法,但我得到零作为计数

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

试试这个,

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