You can use a correlated subquery:

select b.*, 
    case when exists (select 1 from tblfavorite f where f.bookid = b.bookid and f.userid = ?)
        then 1
        else 0
    end as isfavorite
from tblbook b

The question mark represents the id of the user for who you are generating the report. In databases that support evaluating conditions as booleans or integers (such as MySQL or Postgres for example), you can dispense the case expression:

select b.*, 
    exists (select 1 from tblfavorite f where f.bookid = b.bookid and f.userid = ?) as isfavorite
from tblbook b

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top