How to limit values when using distinct

前端 未结 4 1939
暗喜
暗喜 2021-01-22 10:30

PHP

  SELECT DISTINCT bk.title AS Title, bk.year AS Year, aut.authorname AS Author, cat.category AS Category
         FROM book bk 

         JOIN book_category          


        
4条回答
  •  旧巷少年郎
    2021-01-22 10:55

    I think you might want to look into subquery-ing with the group_concat() so please give this a try:

    group_concat() will merge the results of the sub-query and provide you with one string of authors separated by a comma-space like this author1, author2, author3

    SELECT
        DISTINCT bk.title AS Title,
        bk.year AS 'Year',
        (
            SELECT
                group_concat(aut.authorname, ', ')
            FROM
                authors aut
                LEFT OUTER JOIN
                    books_authors bk_aut
                    ON
                        aut.id=bk_aut.author_id
            WHERE
                bk_aut.book_id = bk.book_id
        ) AS Author
    FROM
        book bk
    ORDER BY
        bk.title ASC
    

提交回复
热议问题