Using CASE with subquery

I’m trying to figure out how to recognize no result set from a subquery.

My query looks like this:


		WHERE 
        	(entry_id = '$entry_id'
            OR (
            	entry_id > 
                CASE (SELECT COUNT(entry_id)
                            FROM book_entries 
                            WHERE 
                                entry_id < '$entry_id' AND 
                                book_id='$book_id' AND 
                                chapter_flag = 1 
                                ORDER BY entry_id DESC LIMIT 1)
                
                WHEN 0 THEN 0
                ELSE (SELECT entry_id 
                                    FROM book_entries 
                                    WHERE chapter_flag = 1 
                                    AND entry_id < '$entry_id' 
                                    AND book_id='$book_id' 
                                    ORDER BY entry_id 
                                  DESC LIMIT 1)     
            ))

basically the idea is that, if there’s no previous entry with chapter_flag set to 1, it will get everything > 0, otherwise it stops at the first chapter_flag it finds and pulls all entry_id > that value.

It doesn’t quite work, but hopefully someone could point in the right direction.

Thank you.

You have to set the chapter_flag variables value to 0