简体   繁体   中英

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

İ 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 get 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 , eg

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM