I’m having some trouble working this one out… any suggestions would be very welcome.
I have a table such as the following:
collection_id book_id author
===================================
1 027 derek
2 090 bob
2 102 andy
2 102 bob
Now I want to retrieve the book_id for a book that I know belongs in collection 2 and is authored by andy AND bob (i.e. 102).
A query that includes a WHERE clause for andy AND bob returns nothing because obviously there is only one author per row. A query that includes a WHERE clause for andy OR bob returns the book_id for 090 and 102 because bob is the author of book 090 too.
How would you go about obtaining the right book_id for the book authored by andy AND bob?!
Many thanks for your thoughts!
That can only be solved using subqueries. For example, take a look at the EXISTS keyword.
or, simplified (but not symmetrical):
Alternatively, use
COUNTin a subquery: