[eluser]Firestorm ZERO[/eluser]
Yeah. I notice that it will use too many queries the way I'm using it right now.
This will become more of a problem because in my simple example I only have an "author" column. But there are more columns that have a M-M relationship in what I am doing and therefore more bridge tables and then more queries.
So I did some reading and I think this query will do what I want...
Code:
SELECT book.book_id, book.title, GROUP_CONCAT(author.name)
FROM book
LEFT JOIN book_author
ON book.book_id = author.author_id
LEFT JOIN author
ON book_author.author_id = author.author_id
GROUP BY book_id
Result
Code:
1 Book Title 1 Author 1, Author 2
2 Book Title 2 Author 2
3 Book Title 3 NULL (was not in the book_author table so no author)
So the GROUP BY clause will group the multiple rows. And GROUP_CONCAT will concatenate all the values of the rows it grouped by into one field. I'm assuming it will be a string so I will have explode it by the commas and then do a sort on the arrays.
I think GROUP_CONCAT might be MySQL specific. As well there is one problem. There is a limit to how much GROUP_CONCAT can hold. The default is I think 1024 characters.
Just wondering... for the people who do have Many-To-Many relationships in tables. How are you handling them?