Let’s say I have a table people and a table pets and I’d like to display all people who have pets and all people who don’t have pets. A person can have either no pet or any number of pets.
I can use IF EXISTS, but then I have to use two queries:
select * from person where exists (select * from pet where person.id = pet.owner)
and
select * from person where not exists (select * from pet where person.id = pet.owner)
Is there a way to combine these in a single query, so that I can have a list of person data with an additional column which says if the person has a pet or not? I don’t want to use group by and count, because I don’t need the acutal number of pets, only the info if there is a pet, so I want to merge the EXISTS data of the above two queries into an extra boolean column.
Is it possible to do this?
Alternative solution
http://dev.mysql.com/doc/refman/5.0/en/case-statement.html