111

Thursday 5 September 2019

SQL: 3 tables (book, author, book_author)?

answers1: As you're using mySQL, you should look at the GROUP_CONCAT
function. It can be found at: <br>
<br>
<a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat"
rel="nofollow"class=Clr-b>http://dev.mysql.com/doc/refman/5.0/en/g...</a>
<br>
<br>
<br>
I don't have access to a mySQL database right at this moment that I
can check this works, but the following should do what you're after:
<br>
<br>
SELECT book.[book_id], book.[title], GROUP_CONCAT(author.[name]
SEPARATOR ', ') <br>
FROM (book INNER JOIN book_author <br>
ON book_author.[book_id] = book.[book_id]) <br>
INNER JOIN author.[author_id] <br>
ON author.[author_id] = book_author.[author_id] <br>
WHERE book.[book_id] = '1' <br>
<br>
<br>
Obviously change the where statement to whatever you want/need. <br>
<br>
<br>
Hope this helps! <br>
.
answers2: Yes, you need to use MySQL's GROUP_CONCAT function.

No comments:

Post a Comment