开发者

MySQL showing duplicates in a column

开发者 https://www.devze.com 2023-03-12 02:53 出处:网络
Hi I\'m trying to write a query to get information about the author, title, category and medium. However as the items can be in many mediums and categories, I\'m getting the results appearing duplicat

Hi I'm trying to write a query to get information about the author, title, category and medium. However as the items can be in many mediums and categories, I'm getting the results appearing duplicated in the columns. How can I get the开发者_如何学运维 results so I don't see medium as book,book,book and category as Horror,Fantasy,Fiction. I'm assuming I will need some sort of subquery - if so how would I do it?

SELECT book.bookid, book.author, book.title, group_concat(category.categorydesc), group_concat(medium.mediumdesc)
FROM book
Inner JOIN bookscategories ON book.bookid = bookscategories.bookid
Inner JOIN category ON bookscategories.categoryid = category.categoryid
Inner JOIN booksmediums ON book.bookid = booksmediums.bookid
Inner JOIN medium ON booksmediums.mediumid = medium.mediumid
GROUP BY book.bookid

Thanks Tom


So as stated in comments, solution is to add the DISTINCT keyword in the GROUP_CONCAT() instructions like that:

... book.title, group_concat(DISTINCT category.categorydesc), group_concat(DISTINCT medium.mediumdesc) ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号