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

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

提交回复
热议问题