How to limit values when using distinct

前端 未结 4 1926
暗喜
暗喜 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:37
      SELECT DISTINCT bk.title AS Title, bk.year AS Year, aut.autName AS Author, cat.category AS Category
         FROM book bk 
    
         JOIN book_category bk_cat 
         ON bk_cat.book_id = bk.bookid
    
         JOIN categories cat 
         ON cat.id = bk_cat.category_id
    
    
         JOIN (SELECT GROUP_CONCAT(aut.authorname) as autName FROM authors JOIN books_authors ON books_authors.author_id=authors.author_id WHERE books_authors.book_id=bk.bookid) as aut
    
    
         ORDER BY bk.title ASC
    
    0 讨论(0)
  • 2021-01-22 10:38

    The easiest way to achieve different data only in the columns you want is using GROUP BY clause. By default, it'll group the rows, depending on the value of the column, showing only distinct values so, if you want to group and show only different titles and categories, you should write your query as:

      SELECT bk.title AS Title, bk.year AS Year, aut.authorname AS Author, cat.category AS Category
      FROM book bk 
    
      JOIN book_category bk_cat 
      ON bk_cat.book_id = bk.bookid
    
      JOIN categories cat 
      ON cat.id = bk_cat.category_id
    
      JOIN books_authors bk_aut 
      ON bk_aut.book_id = bk.bookid
    
      JOIN authors aut
      ON aut.id = bk_aut.author_id
    
      GROUP BY  bk.title, cat.category
      ORDER BY bk.title ASC
    

    As you may see, no DISTINCT is used, but you'll get all books with distincts title and categories. The more fields you added into the GROUP BY clause, the more distinct data you'd get.

    Same way, if you only wanted list books by title, you should only leave bk.title in the GROUP BY clause

    0 讨论(0)
  • 2021-01-22 10:49

    DISTINCT works on the entire row, not a single column. The only way to ensure not repeating rows is to exclude category column from the select.

    If your objective is to list all categories, you will have to either look up categories as you're looping over the query result set during output (not very efficient though) OR process your result set in the code before you output it by collapsing repeating rows and merging/concatenating category values into a string.

    You could try some fancy SQL with subqueries to convert categories into a single delimited string but that will sure make your code hard to read and query hard to debug.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题