我有两张桌子
Product(id, ..., other attribute...)
与
有关UserLikeProduct(id, product_id, like_at, user_id)
如何选择最受欢迎的产品?
答案 0 :(得分: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