stijn0713 Posted February 4, 2013 Share Posted February 4, 2013 2 tables: book and author. book_id title 1 a book title A book can have multiple authors. book_id author 1 author1 1 author2 Suppose i want to show the title of the book (saved in the book table) in one column and all the authors (in the author table) as a comma seperated list in another column. What would be an easy way/ query to retrieve all the authors in a comma-seperated list. Is this possible with a query? i'm trying to find a way of doing it in 1 query, not retrieving all books and than another query for the authors. Quote Link to comment https://forums.phpfreaks.com/topic/274010-joins/ Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 You don't really want to save them as a comma-delimited list, as this is indicative of bad database design. What you really want to do, is to set up a many-to-many relationship between the two tables "books" and "authors". There are plenty of tutorials on how you do this, so a search on the net for "mysql many to many relationship tutorial" should give you all the information you need. Quote Link to comment https://forums.phpfreaks.com/topic/274010-joins/#findComment-1410040 Share on other sites More sharing options...
kicken Posted February 4, 2013 Share Posted February 4, 2013 Mysql has a GROUP_CONCAT function you can use to generate the comma-separated list SELECT title, GROUP_CONCAT(author) FROM books b INNER JOIN authors a ON b.book_id=a.book_id GROUP BY title For other DB's that don't have such a function you'd just have to select the title and authors together then group them in your program later on. Quote Link to comment https://forums.phpfreaks.com/topic/274010-joins/#findComment-1410049 Share on other sites More sharing options...
Psycho Posted February 4, 2013 Share Posted February 4, 2013 You don't really want to save them as a comma-delimited list, as this is indicative of bad database design. I think you jumped to a conclusion. The sample DB records and his text seem to clearly indicate that his DB design is appropriate. Suppose i want to show the title of the book (saved in the book table) in one column and all the authors (in the author table) as a comma seperated list in another column. Quote Link to comment https://forums.phpfreaks.com/topic/274010-joins/#findComment-1410051 Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 Ah, yes. I did jump the gun a little bit. Read it as "save" not "show". Sorry about that. That said, it's still not a m2m relationship described in the OP. Which means if an author has written more than one book, then that author would be saved twice in the authors table. Which is why I recommended to read up on m2m relationships. Quote Link to comment https://forums.phpfreaks.com/topic/274010-joins/#findComment-1410059 Share on other sites More sharing options...
stijn0713 Posted February 7, 2013 Author Share Posted February 7, 2013 thanks kicken Quote Link to comment https://forums.phpfreaks.com/topic/274010-joins/#findComment-1410827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.