0

I get error When I use subquery in select and from clause
Error: https://www.awesomescreenshot.com/image/3069216/0505cad495528e9f9af2281ea281415c.

Here what I have done so far:

SELECT m.id, m.title_name, m.release_date, m.plot, m.poster, 
  (SELECT count(*) FROM users_ratings WHERE type_id = m.id AND type_name = 'movies' LIMIT 1) count_user_rating 
FROM (SELECT u.* FROM users u
      LEFT JOIN users_ratings ur ON ur.user_id = u.id
      WHERE ur.type_id = 2
      AND ur.type_name = 'movies') u
LEFT JOIN users_ratings ur ON ur.user_id = u.id
LEFT JOIN movies m ON m.id = ur.type_id
WHERE ur.type_name = 'movies'

UNION

SELECT s.id, s.title_name, s.first_air_date release_date, s.plot, s.poster, 
  (SELECT count(*) FROM users_ratings WHERE type_id = s.id AND type_name = 'series' LIMIT 1) count_user_rating 
FROM (SELECT u.* FROM users u
      LEFT JOIN users_ratings ur ON ur.user_id = u.id
      WHERE ur.type_id = 2
      AND ur.type_name = 'movies') u
LEFT JOIN users_ratings ur ON ur.user_id = u.id
LEFT JOIN series s ON s.id = ur.type_id
WHERE ur.type_name = 'series'
6
  • That's because the subquery count_user_rating is not fitting well with the syntax. It is begin evaluated as a table and you seem to want to use it as a column. Commented Dec 30, 2017 at 2:31
  • ok so how to fix that? Commented Dec 30, 2017 at 2:32
  • Since it is going to evaluate as one value from the COUNT(*) function, although in the form of a single-row table, are you trying to populate that column with same value across all the rows? Commented Dec 30, 2017 at 2:34
  • @Vashi yes correct Commented Dec 30, 2017 at 2:37
  • @Vashi - Count will not be same for all the records. Check there is a condition in where clause. Count will be same for each id Commented Dec 30, 2017 at 2:45

1 Answer 1

1

Not sure whats wrong in your query, seems fine to me. But here is another way to achieve it. Try this

SELECT m.id,
       m.title_name,
       m.release_date,
       m.plot,
       m.poster,
       t.count_user_rating
FROM   (SELECT u.*
        FROM   users u
               LEFT JOIN users_ratings ur
                      ON ur.user_id = u.id
        WHERE  ur.type_id = 2
               AND ur.type_name = 'movies') u
       LEFT JOIN users_ratings ur
              ON ur.user_id = u.id
       LEFT JOIN movies m
              ON m.id = ur.type_id
       LEFT JOIN (SELECT type_id,
                         Count(*) AS count_user_rating
                  FROM   users_ratings
                  WHERE  NAME = 'movies'
                  GROUP  BY type_id) t
              ON t.type_id = m.id
WHERE  ur.type_name = 'movies'
UNION
SELECT s.id,
       s.title_name,
       s.first_air_date release_date,
       s.plot,
       s.poster,
       t.count_user_rating
FROM   (SELECT u.*
        FROM   users u
               LEFT JOIN users_ratings ur
                      ON ur.user_id = u.id
        WHERE  ur.type_id = 2
               AND ur.type_name = 'movies') u
       LEFT JOIN users_ratings ur
              ON ur.user_id = u.id
       LEFT JOIN series s
              ON s.id = ur.type_id
       LEFT JOIN (SELECT type_id,
                         Count(*) AS count_user_rating
                  FROM   users_ratings
                  WHERE  NAME = 'series'
                  GROUP  BY type_id) t
              ON t.type_id = s.id
WHERE  ur.type_name = 'series' 
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I was thinking. Should be fine.
@Pரதீப் I tried your solution and it's working like charme

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.