2012-12-31 172 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