SQL grouping by all the columns

后端 未结 9 1529
囚心锁ツ
囚心锁ツ 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

    The DISTINCT Keyword


    I believe what you are trying to do is:

    SELECT DISTINCT * FROM MyFooTable;
    

    If you group by all columns, you are just requesting that duplicate data be removed.

    For example a table with the following data:

     id |     value      
    ----+----------------
      1 | foo
      2 | bar
      1 | foo
      3 | something else
    

    If you perform the following query which is essentially the same as SELECT * FROM MyFooTable GROUP BY * if you are assuming * means all columns:

    SELECT * FROM MyFooTable GROUP BY id, value;

     id |     value      
    ----+----------------
      1 | foo
      3 | something else
      2 | bar
    

    It removes all duplicate values, which essentially makes it semantically identical to using the DISTINCT keyword with the exception of the ordering of results. For example:

    SELECT DISTINCT * FROM MyFooTable;

     id |     value      
    ----+----------------
      1 | foo
      2 | bar
      3 | something else
    

提交回复
热议问题