简体   繁体   中英

How to get the select the sum of a column JOINing with another table where elementxof table1 = elementx of table2?[special case]

I have two tables named videos and rating .

The first table

videos

uploader           video_id

james              ac0255
james              ue2145
isabell            qw2378

The second table:

rating

video_id           score

ac0255             4
qw2378             2
ue2145             6

I want to store in variable x the sum of the score of all the videos uploaded by james . Can anyone suggest an SQL query for it?

Try with this:

SELECT SUM(rating.score)
FROM videos
INNER JOIN rating
ON videos.uploader = rating.video_id
WHERE videos.uploader = 'james';

A simple join will do.

SELECT sum(r.score)
FROM videos v
JOIN rating r ON (v.video_id = r.video_id)
WHERE v.uploader = 'james';

In order to get the sum of score for James , you need to use JOINS and SUM function of mysql

SELECT SUM(Rating.score)
FROM videos as Videos
INNER JOIN rating AS Rating
ON Videos.uploader = Rating.video_id
WHERE Videos.uploader = 'james';

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