I have three tables movie_request
,movie_request_2
and movie_request_3
I want to get count() from each table and return as one row, with each table count() as a column.
movie_request
is free_cnt,
movie_request_2
is plus_cnt,
movie_request_3
is visitor_cnt
I expect results like
movie_id | movie_title | free_cnt | plus_cnt | visitor_cnt
10 | Test | 5 | 10 | 15
11 | Test2 | 6 | 11 | 25
Here is my query
SELECT movie_title,movie_id,MAX(x.free_cnt) as free_cnt, MAX(x.plus_cnt) as plus_cnt, MAX(x.visitor_cnt) as visitor_cnt, (MAX(x.free_cnt) + MAX(x.plus_cnt) + MAX(x.visitor_cnt)) AS total
FROM (
SELECT a1.id as movie_id, a1.movie_title as movie_title, 0 as plus_cnt, 0 as visitor_cnt, COUNT(*) AS free_cnt
FROM movie_request a1
GROUP BY a1.movie_title
UNION ALL
SELECT a2.id as movie_id, a2.movie_title as movie_title, 0 as free_cnt, 0 as visitor_cnt, COUNT(*) AS plus_cnt
FROM movie_request_2 a2
GROUP BY a2.movie_title
UNION ALL
SELECT a3.id as movie_id, a3.movie_title as movie_title, 0 as plus_cnt, 0 as free_cnt, COUNT(*) AS visitor_cnt
FROM movie_request_3 a3
GROUP BY a3.movie_title
) AS x
GROUP BY movie_title
I keep getting results like this
movie_id | movie_title | free_cnt | plus_cnt | visitor_cnt
10 | Test | 5 | 0 | 0
11 | Test2 | 6 | 0 | 0
I've tested results separetely and they all return results.
How do i solve?