posted 22 years ago
I am using a mysql database for a bookshop project i am working on. I am trying to run a select query to retrieve a list of book ISBNs, titles and authors that meet certain criteria.
My problem is this: I have set my database up so that there is a many to many relationship between authors and books, so I have 3 tables:
Books
-----
ISBN
Title
etc
Authors
-------
Author_ID
Forename
Surname
Books_Authors
-------------
ISBN
Author_ID
Now, when I run a select query like SELECT Books.ISBN, title, Forename, Surname FROM Books,Authors,Books_Authors WHERE Books.ISBN=Books_Authors.ISBN AND Authors.Author_ID=Books_Authors.Author_ID, I get all the information I need, but of course if a book has multiple authors, there are multiple rows in the result set.
When looping through the result set, I basically want to get each book's details and its authors and display them as *one* record for each book.
How can I tell whether the row I am processing is a "new book" or just contains details on another author for the previous book?
Or, would I be better off doing one query to get the list of ISBNs and titles etc and then for each ISBN retrieved, do a separate query to find the relevant author(s) for that ISBN? That seems a simpler approach to a newbie like me, but something tells me that using a whole load of queries instead of 1 is very inefficient!
Any advice would be greatly appreciated...