對資料庫表中的某一欄位去重分組排序,某一欄位

來源:互聯網
上載者:User

對資料庫表中的某一欄位去重分組排序,某一欄位

1、問題背景

   某資料庫t_tab_ab中有兩個欄位a和b,如下所示:

   查詢前:                 查詢後:

   a     b                  a     b

   1     2                  1     2

   1     3                  2     3

   1     4                  3     4

   1     5

   2     3

   2     4

   2     5

   3     4

   3     5


2、解決思路

   通過查詢a和b中的最小值,對a進行分組和排序

   select t.a,min(t.b) from t_tab_ab group by t.a order by t.a


3、實現結果

   a     b

   1     2

   2     3

   3     4


sql將表的查詢結果按照某個欄位分組後再按照另一個個欄位排序,對於每一組的資料再設定自增欄位

select * from table group by row1 order by row2
再設定自增欄位,這個應該在資料錄入的時候通過觸發器實現,本人搞不定
 
我想在資料庫中的一個表中查詢出全部資料 並且按照一個分組與排序

select
分組列1,分組列2...,
彙總列1,彙總列2...
from 表
group by 分組列1,分組列2...
order by 分組列或彙總列
--------
分組查詢中,做為查詢結果顯示的列,必須是用於分組的列,或者彙總的列.
比如:sum(),count(),avg()等等.
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.