1

我想从一个表中选择列,并根据另外两个表的连接进行计数,我该怎么做?

这是我写的计数查询:

   SELECT COUNT(*) AS num_review FROM approved_reviews  m, reviews u where 
   m.review_id_=u.id  and u.item_id =? and m.approved=1 

我想加入它

select item_id, item_name, item_price from items ?

我该怎么做?

4

2 回答 2

0
select items.item_id, item_name, item_price, COALESCE(t.cnt ,0)         
from items join (
                  SELECT u.item_id , COUNT(*) cnt
                  FROM approved_reviews  m, reviews u 
                  WHERE m.review_id_=u.id and m.approved=1
                  GROUP BY u.item_id 
                ) t on items.item_id = t.item_id
于 2012-11-03T11:21:18.423 回答
0
select * 
from
 (SELECT COUNT(*) AS num_review, u.item_id FROM approved_reviews  m, reviews u where 
   m.review_id_=u.id  and u.item_id =? and m.approved=1 group by u.item_id) count_sub, 
(select item_id, item_name, item_price from items) item_sub
where item_sub.item_id = count_sub.item_id
于 2012-11-03T11:21:42.227 回答