Can't use where clause on correlated columns.

Posted by Keyo on Stack Overflow See other posts from Stack Overflow or by Keyo
Published on 2010-06-01T05:35:26Z Indexed on 2010/06/01 5:43 UTC
Read the original article Hit count: 264

Filed under:
|

I want to add a where clause to make sure video_count is greater than zero. Only categories which are referenced once or more in video_category.video_id should be returned.

Because video_count is not a field in any table I cannot do this.

Here is the query.

SELECT category . * , (
        SELECT COUNT( * )
        FROM video_category
        WHERE video_category.category_id = category.category_id
        ) AS 'video_count'
        FROM category
        WHERE category.status = 1
    AND video_count > '0'
        AND publish_date < NOW()
        ORDER BY updated DESC;

Thanks for the help.

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql