完美解決distinct中使用多個欄位的方法

2021-06-01 22:56:07 字數 325 閱讀 5712

眾所周知,distinct可以列出不重複的記錄,對於單個字段來說distinct使用比較簡單,但是對於多個字段來說,distinct使用起來會使人發狂。而且貌似也沒有見到微軟對distinct使用多字段的任何說明。下面就提供了一種方法可以在使用distinct的時候同時使用多個字段。

select 要使用欄位1,要使用欄位2 from 表名 where id in (select min(id) from 表名 group by 不重複欄位名)

例: select byid,id from bbs where id in (select min(id) from bbs group by byid)

在Django中使用DISTINCT

有時候想用distinct去掉queryset中的重複項,看django文章中是這麼說的 author.objects.distinct entry.objects.order by pub date distinct pub date entry.objects.order by blog dis...

解決distinct中使用多個欄位的方法

眾所周知,distinct可以列出不重複的記錄,對於單個字段來說distinct使用比較簡單,但是對於多個字段來說,distinct使用起來會使人發狂。而且貌似也沒有見到微軟對distinct使用多字段的任何說明。下面就提供了一種方法可以在使用distinct的時候同時使用多個字段。select 要...

完美解決SQL語句distinct的多列問題

distinct 多列問題結合group by的解決方法 表 table1 id regname postionsn personsn 1 山東齊魯製藥 223 2 2 山東齊魯製藥 224 2 3 北京城建公司 225 2 4 科技公司 225 2 我想獲得結果是 id regname posti...