I’m implementing a book search function based on authors. I should return a query result that contains all the books written by the queried author. However, it is possible that a query for certain author names returns multiple results (e.g., a query of “Smith, W” might match “Smith, Wesson” and “Smith, Will”).
So, my problem is how to “concatenate” all the books written by these different authors. If I don’t consider the possibility of multiple authors matching a query, I’d go about something like this (in pseudocode, as my real code is quite messy right now):
- search author table for the author
- matching the query
- get author’s authorid
- search book table for book records with the same authorid
However, with the possibility of multiple authors, I have something like this in mind:
// search author table for authors matching the query
foreach(author_match as am){
// search book table for book records with authorid=am.authorid
// Is there such thing as this? :\
book_results += all the rows returned by previous query
}
return book_results;
I’m doing this in PHP (with CodeIgniter Framework) and MySQL. Is there any function/operator that will allow me to do this? Yes, I’ve tried +=, even if I wasn’t expecting much from it, to an ugly output.
Again, my apologies for the pseudocode. I’ll try to clean-up my code and edit ASAP but if an answer comes before that, it’d be just as awesome. Thanks.
I agree with Michael, it seems you need either an
INNER JOINor aLEFT JOIN.You say you are using Codeigniter so here is a Codeigniter specific example:
Also see: