SQL/mysql - Select distinct/UNIQUE but return all columns?

前端 未结 18 939
忘掉有多难
忘掉有多难 2020-11-22 12:08
SELECT DISTINCT field1, field2, field3, ......   FROM table

I am trying to accomplish the following sql statement but I want it to return all colum

相关标签:
18条回答
  • 2020-11-22 12:25

    From the phrasing of your question, I understand that you want to select the distinct values for a given field and for each such value to have all the other column values in the same row listed. Most DBMSs will not allow this with neither DISTINCT nor GROUP BY, because the result is not determined.

    Think of it like this: if your field1 occurs more than once, what value of field2 will be listed (given that you have the same value for field1 in two rows but two distinct values of field2 in those two rows).

    You can however use aggregate functions (explicitely for every field that you want to be shown) and using a GROUP BY instead of DISTINCT:

    SELECT field1, MAX(field2), COUNT(field3), SUM(field4), .... FROM table GROUP BY field1
    
    0 讨论(0)
  • 2020-11-22 12:25

    Add GROUP BY to field you want to check for duplicates your query may look like

    SELECT field1, field2, field3, ......   FROM table GROUP BY field1
    

    field1 will be checked to exclude duplicate records

    or you may query like

    SELECT *  FROM table GROUP BY field1
    

    duplicate records of field1 are excluded from SELECT

    0 讨论(0)
  • 2020-11-22 12:27

    Found this elsewhere here but this is a simple solution that works:

     WITH cte AS /* Declaring a new table named 'cte' to be a clone of your table */
     (SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY val1 DESC) AS rn
     FROM MyTable /* Selecting only unique values based on the "id" field */
     )
     SELECT * /* Here you can specify several columns to retrieve */
     FROM cte
     WHERE rn = 1
    
    0 讨论(0)
  • 2020-11-22 12:31

    If I understood your problem correctly, it's similar to one I just had. You want to be able limit the usability of DISTINCT to a specified field, rather than applying it to all the data.

    If you use GROUP BY without an aggregate function, which ever field you GROUP BY will be your DISTINCT filed.

    If you make your query:

    SELECT * from table GROUP BY field1;
    

    It will show all your results based on a single instance of field1.

    For example, if you have a table with name, address and city. A single person has multiple addresses recorded, but you just want a single address for the person, you can query as follows:

    SELECT * FROM persons GROUP BY name;
    

    The result will be that only one instance of that name will appear with its address, and the other one will be omitted from the resulting table. Caution: if your fileds have atomic values such as firstName, lastName you want to group by both.

    SELECT * FROM persons GROUP BY lastName, firstName;
    

    because if two people have the same last name and you only group by lastName, one of those persons will be omitted from the results. You need to keep those things into consideration. Hope this helps.

    0 讨论(0)
  • 2020-11-22 12:31

    Great question @aryaxt -- you can tell it was a great question because you asked it 5 years ago and I stumbled upon it today trying to find the answer!

    I just tried to edit the accepted answer to include this, but in case my edit does not make it in:

    If your table was not that large, and assuming your primary key was an auto-incrementing integer you could do something like this:

    SELECT 
      table.*
    FROM table
    --be able to take out dupes later
    LEFT JOIN (
      SELECT field, MAX(id) as id
      FROM table
      GROUP BY field
    ) as noDupes on noDupes.id = table.id
    WHERE
      //this will result in only the last instance being seen
      noDupes.id is not NULL
    
    0 讨论(0)
  • 2020-11-22 12:34

    Just include all of your fields in the GROUP BY clause.

    0 讨论(0)
提交回复
热议问题