Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > SELECT * FROM users
- +----+---------------------------+
- | id | email |
- +----+---------------------------+
- | 5 | abcdefghijklmno@gmail.com |
- +----+---------------------------+
- > SELECT * FROM movies
- +----+---------+---------+--------------+
- | id | title | user_id | total_watches|
- +----+---------+---------+--------------+
- | 1 | X-men | 1 | 1 |
- | 2 | Blade | 1 | 1 |
- | 3 | Goonies | 1 | 1 |
- +----+---------+---------+--------------+
- > SELECT * FROM sessions
- +----+---------+---------+------------+
- | id | user_id | show_id | total_time |
- +----+---------+---------+------------+
- | 1 | 1 | 1 | 5 |
- | 2 | 1 | 1 | 30 |
- | 3 | 1 | 1 | 5 |
- +----+---------+---------+------------+
- +----+---------------------------+---------------+----------------+
- | id | email | total_time | total_watches |
- +----+---------------------------+---------------+----------------+
- | 5 | abcdefghijklmno@gmail.com | 40 | 3 |
- +----+---------------------------+---------------+----------------+
- SELECT users.id, users.email, SUM(movies.total_watches) AS total_watches, SUM(sessions.total_time) AS total_time
- FROM users
- JOIN movies ON users.id = movies.user_id
- JOIN sessions ON users.id = sessions.user_id
- GROUP BY user.id
- +------------------+---------------+---------------+
- | email | total_watches | total_time |
- +------------------+---------------+---------------+
- | abcdef@gmail.com | 9 | 120 |
- +------------------+---------------+---------------+
Add Comment
Please, Sign In to add comment