在我的应用程序中,每个用户都可以有自己的产品愿望列表。我想要五张最常用的唱片但是..。Wishlist表获取列:
位置是产品在愿望列表中显示的顺序。每个位置都能得到积分。积在第一位得到9分,第二个8分(.)第十位,每隔一段时间结束,得到零分。
换句话说,我想得到5条最高和值的记录(count() * position_point),由product_id记录
这在sql中是可能的吗?或者我应该使用php来获得正确的记录吗?
类似于:
select product_id, CASE position WHEN 1 THEN 9 * (select count(id) as countie from product_wishlist b where position <10 and a.product_id = b.product_id and a.position = b.position group by product_id) end as summary
from product_wishlist a
会正确地工作并且是相当优化的吗?只是增加了剩下的条件?
编辑:并在product_id上区别开来
发布于 2015-02-25 16:41:29
您可以写得更简单一些:
select product_id,
sum(greatest(10 - position, 0)) as summary
from product_wishlist pw
group by product_id
order by summary desc
limit 5;
https://stackoverflow.com/questions/28729397
复制相似问题