0

可能真的很简单,但我无法弄清楚。

这是我的 SQL

select ag.product_id, COUNT(reviewer) as review_number    
from PRODUCT ag    
left join RATINGANDREVIEW rr on rr.product_id = ag.product_id
where ag.product_id = 123
group by ag.product_id

这给了我对一种产品的评论总数,如下所示:

产品编号:123

评论编号:3

但我现在希望审稿人的名字也像这样:

产品编号:123

评论编号:3

审稿人:鲍勃·史密斯

产品编号:123

评论编号:3

审稿人:彼得·琼斯

产品编号:123

评论编号:3

审稿人:简·格林

我该怎么做呢?

4

2 回答 2

1

你可以这样做:

select 
ag.product_id, 
reviewer,

(select COUNT(reviewer) 
from PRODUCT ag
left join RATINGANDREVIEW rr on rr.product_id = ag.product_id
where ag.product_id = 123) as review_number

from PRODUCT ag
left join RATINGANDREVIEW rr on rr.product_id = ag.product_id
where ag.product_id = 123
于 2013-02-18T18:41:22.333 回答
0

像这样试试

select ag.product_id, COUNT(reviewer) as review_number, rr.reviewer_name
from PRODUCT ag    
left join RATINGANDREVIEW rr on rr.product_id = ag.product_id
where ag.product_id = 123
group by ag.product_id,rr.reviewer_name
于 2013-02-18T18:38:29.090 回答