Question:
How do you compare (< or >) the result of one column, against the result of another column of two select statements?
More details:
I need to get a list only of the sold out movies. To do this, I need to check if
the number on the rented_movie_count on the first select statement is equal or greater than the number on the available_movie_count on the second select statement.
First Statement
result of the
select MOVIE_ID, count(MOVIE_ID) as rented_movies_count
from MOVIE_QUEUE
WHERE STATUS_ID=2
group by MOVIE_ID;
Second Statement
select MOVIE_ID, count(MOVIE_ID) as available_movie_count
from dvd
group by MOVIE_ID;
I am still very new to this and I am trying to learn, even suggestions about what kind of
syntax (operators, expressions, etc) should I use will help at least point me on a a directions to do more research. Thanks in advance
Given your database structure, a possible solution would be
I’ve set up an example on http://www.sqlfiddle.com/#!4/3fc59/11 to test. But I guess that’s more or less what you’ve come up with.
But I somewhat doubt that your database structure is really optimal!? Do you really store more than one record for a singe
movie_idin the tabledvd?Wouldn’t it be more useful to have exactly one record per
movie_idindvdalong with some movie attributes and store the rental status of the dvds inmovie_queue.status_id?If
movie_queue.status_idis defined as sayThen your query would be much more easy:
See for example http://www.sqlfiddle.com/#!4/13739/1