SQL grouping by all the columns

后端 未结 9 1491
囚心锁ツ
囚心锁ツ 2020-12-29 02:04

Is there any way to group by all the columns of a table without specifying the column names? Like:

select * from table group by *
相关标签:
9条回答
  • 2020-12-29 02:37

    If you are using SqlServer the distinct keyword should work for you. (Not sure about other databases)

    declare @t table (a int , b int)
    
    insert into @t (a,b) select 1, 1
    insert into @t (a,b) select 1, 2
    insert into @t (a,b) select 1, 1
    
    select distinct * from @t
    

    results in

    a b
    1 1
    1 2
    
    0 讨论(0)
  • 2020-12-29 02:42

    Short answer: no. GROUP BY clauses intrinsically require order to the way they arrange your results. A different order of field groupings would lead to different results.

    Specifying a wildcard would leave the statement open to interpretation and unpredictable behaviour.

    0 讨论(0)
  • 2020-12-29 02:45

    No because this fundamentally means that you will not be grouping anything. If you group by all columns (and have a properly defined table w/ a unique index) then SELECT * FROM table is essentially the same thing as SELECT * FROM table GROUP BY *.

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