SELECT DISTINCT field1, field2, field3, ......
FROM table;

我试图完成以下SQL语句,但我希望它返回所有列。 这可能吗?

就像这样:

SELECT DISTINCT field1, * 
FROM table;

当前回答

将GROUP BY添加到要检查重复的字段 您的查询可能看起来像

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

将检查Field1以排除重复记录

或者你可能会问

SELECT *  FROM table GROUP BY field1

字段1的重复记录被排除在SELECT中

其他回答

Try

SELECT table.* FROM table 
WHERE otherField = 'otherValue'
GROUP BY table.fieldWantedToBeDistinct
limit x
SELECT  c2.field1 ,
        field2
FROM    (SELECT DISTINCT
                field1
         FROM   dbo.TABLE AS C
        ) AS c1
        JOIN dbo.TABLE AS c2 ON c1.field1 = c2.field1

这样只需要1个查询就可以得到2个唯一的列 select Distinct col1,col2 from '{path}' group by col1,col2 如果需要,可以增加列

你可以用with子句来实现。

例如:

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

这还允许您只选择WITH子句查询中选择的行。

对于SQL Server,您可以使用dense_rank和其他窗口函数来获取指定列上具有重复值的所有行和列。这里有一个例子……

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

这是对col1、col2和col3的每个不同组合进行行计数。