Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/274010-joins/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/274010-joins/#findComment-1410040
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/274010-joins/#findComment-1410049
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/274010-joins/#findComment-1410051
Share on other sites

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. ;)

Link to comment
https://forums.phpfreaks.com/topic/274010-joins/#findComment-1410059
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.