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

前端 未结 18 935
忘掉有多难
忘掉有多难 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:15

    That's a really good question. I have read some useful answers here already, but probably I can add a more precise explanation.

    Reducing the number of query results with a GROUP BY statement is easy as long as you don't query additional information. Let's assume you got the following table 'locations'.

    --country-- --city--
     France      Lyon
     Poland      Krakow
     France      Paris
     France      Marseille
     Italy       Milano
    

    Now the query

    SELECT country FROM locations
    GROUP BY country
    

    will result in:

    --country--
     France
     Poland
     Italy
    

    However, the following query

    SELECT country, city FROM locations
    GROUP BY country
    

    ...throws an error in MS SQL, because how could your computer know which of the three French cities "Lyon", "Paris" or "Marseille" you want to read in the field to the right of "France"?

    In order to correct the second query, you must add this information. One way to do this is to use the functions MAX() or MIN(), selecting the biggest or smallest value among all candidates. MAX() and MIN() are not only applicable to numeric values, but also compare the alphabetical order of string values.

    SELECT country, MAX(city) FROM locations
    GROUP BY country
    

    will result in:

    --country-- --city--
     France      Paris
     Poland      Krakow
     Italy       Milano
    

    or:

    SELECT country, MIN(city) FROM locations
    GROUP BY country
    

    will result in:

    --country-- --city--
     France      Lyon
     Poland      Krakow
     Italy       Milano
    

    These functions are a good solution as long as you are fine with selecting your value from the either ends of the alphabetical (or numeric) order. But what if this is not the case? Let us assume that you need a value with a certain characteristic, e.g. starting with the letter 'M'. Now things get complicated.

    The only solution I could find so far is to put your whole query into a subquery, and to construct the additional column outside of it by hands:

    SELECT
         countrylist.*,
         (SELECT TOP 1 city
         FROM locations
         WHERE
              country = countrylist.country
              AND city like 'M%'
         )
    FROM
    (SELECT country FROM locations
    GROUP BY country) countrylist
    

    will result in:

    --country-- --city--
     France      Marseille
     Poland      NULL
     Italy       Milano
    
    0 讨论(0)
  • 2020-11-22 12:16

    For SQL Server you can use the dense_rank and additional windowing functions to get all rows AND columns with duplicated values on specified columns. Here is an example...

    with t as (
        select col1 = 'a', col2 = 'b', col3 = 'c', other = 'r1' union all
        select col1 = 'c', col2 = 'b', col3 = 'a', other = 'r2' union all
        select col1 = 'a', col2 = 'b', col3 = 'c', other = 'r3' union all
        select col1 = 'a', col2 = 'b', col3 = 'c', other = 'r4' union all
        select col1 = 'c', col2 = 'b', col3 = 'a', other = 'r5' union all
        select col1 = 'a', col2 = 'a', col3 = 'a', other = 'r6'
    ), tdr as (
        select 
            *, 
            total_dr_rows = count(*) over(partition by dr)
        from (
            select 
                *, 
                dr = dense_rank() over(order by col1, col2, col3),
                dr_rn = row_number() over(partition by col1, col2, col3 order by other)
            from 
                t
        ) x
    )
    
    select * from tdr where total_dr_rows > 1
    

    This is taking a row count for each distinct combination of col1, col2, and col3.

    0 讨论(0)
  • 2020-11-22 12:16
    SELECT *
    FROM tblname
    GROUP BY duplicate_values
    ORDER BY ex.VISITED_ON DESC
    LIMIT 0 , 30
    

    in ORDER BY i have just put example here, you can also add ID field in this

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

    You can do it with a WITH clause.

    For example:

    WITH c AS (SELECT DISTINCT a, b, c FROM tableName)
    SELECT * FROM tableName r, c WHERE c.rowid=r.rowid AND c.a=r.a AND c.b=r.b AND c.c=r.c
    

    This also allows you to select only the rows selected in the WITH clauses query.

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

    SELECT DISTINCT FIELD1, FIELD2, FIELD3 FROM TABLE1 works if the values of all three columns are unique in the table.

    If, for example, you have multiple identical values for first name, but the last name and other information in the selected columns is different, the record will be included in the result set.

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

    It can be done by inner query

    $query = "SELECT * 
                FROM (SELECT field
                    FROM table
                    ORDER BY id DESC) as rows               
                GROUP BY field";
    
    0 讨论(0)
提交回复
热议问题