开发者

how to sum values at the same time getting all rows in mysql

开发者 https://www.devze.com 2023-03-08 07:11 出处:网络
İ have a video table named videos and here these are the columns : vidv_uidv_viewv_titlev_info 14556487foofoo

İ have a video table named videos and here these are the columns :

vid    v_uid    v_view   v_title   v_info
1       45      56487     foo       foo
2       45      455
3       45      98989
...

My condition will be " where v_uid = 45 " and i want to 开发者_如何学Goget rows belongs to this id "45" and sum this users video views as total views.

thanks.


I read the question as you want to get all rows and a sum of those rows in one query.

You could do this all in one hit using WITH ROLLUP, e.g.

select *,sum(v_view) as total from videos 
where v_uid=45 
group by vid with rollup;

Bit hacky, as we don't really need the group by (which is why I'm grouping on the row id vid). What you'll end up with all the individual rows, and a final row with the total sum in it (it will have a NULL vid too, which might help identify it)

If you just want a sum of the views, then you can execute something much simpler

select sum(v_view) as total from videos where v_uid=45;
0

精彩评论

暂无评论...
验证码 换一张
取 消