现在要做一个求积分的功能。用户上传一个图片一分,上传一个视频1分,上传一个音乐一分。现在有3个表分别存储图片,音乐,视频:(如上)
我应该怎么写sql语句实现查询出每个用户的积分呢?最好能帮我写下代码。或者,给点好的建议,谢谢。 ------最佳解决方案-------------------- select userid,SUM(integral) as integral
from (
select userid,COUNT(1) as integral
from endurervideo
group by userid
union all
select userid,COUNT(1)
from endurermusic
group by userid
union all
select userid,COUNT(1)
from album
group by userid) as a
group by userid
;with cte as(
select userid, count(1) tsum from endurervideo group by userid union all
select userid,count(1) tsum from endurermusic group by userid union all
select userid, count(1) tsum from album group by userid
)
select userid,sum(tsum) sSum from cte group by userid