0

我有两张桌子

Product(id, ..., other attribute...)

与...有关系

UserLikeProduct(id, product_id, like_at, user_id)

如何选择收到最多赞的产品?

4

1 回答 1

0

首先,您需要构建一个用户喜欢从高到低排序的结果

select product_id,count(*) as TotLikes
from UserLikeProduct
group by Product_id

现在将它与产品表连接起来

select p.Product_id,tl.TotLikes * 
from Products p
join *
(     select product_id,count(*) as TotLikes
    from UserLikeProduct
    group by Product_id

) tl on tl.Product_id=p.Product_id
order by 2 DESC
于 2013-07-11T21:16:59.120 回答