Ok. I'm updating this so that people who come after me (in years to come
) will see how I solved it.
My final query was:
Code:
SELECT books.isbn AS ISBN, books.title AS Title, books.authors AS Authors, books.edition AS Edition,
factual.isbn AS FactualEntryCount,
grammar.isbn AS GrammarYesNo,
for_sale.isbn AS ForSaleBookCount,
Avg(ratings.rating) AS Average_Rating
FROM books
LEFT JOIN factual
ON books.isbn = factual.isbn
LEFT JOIN grammar
ON books.isbn = grammar.isbn
LEFT JOIN for_sale
ON books.isbn = for_sale.isbn
LEFT JOIN ratings
ON books.isbn = ratings.isbn
WHERE if ($search_isbn){$newsql_1=$newsql_1 . " AND books.isbn = '" . $search_isbn ."'";}
if ($search_title){$newsql_1=$newsql_1 . " AND books.title = '" . $search_title ."'";}
if ($search_authors){$newsql_1=$newsql_1 . " AND books.authors = '" . $search_authors ."'";}
if ($search_edition){$newsql_1=$newsql_1 . " AND books.edition = '" . $search_edition ."'";}
if ($search_category){$newsql_1=$newsql_1 . " AND (books.category1 = '" . $search_category ."'"
. " OR books.category2 = '" . $search_category ."'"
. " OR books.category3 = '" . $search_category ."')";}
$newsql_1=$newsql_1 . "GROUP BY books.isbn, books.title, books.authors, books.edition";
The output is packaged into a table that gives all the "books" table details, whether there are corresponding factual, grammar or for_sale entries in the respective tables and the average rating for each book (or no rating).
Using if-else users can click through to details of the factual, grammar or for_sale tables as the isbn is wrapped up in the "yes".
Bookmarks