SQL DISTINCT 用法(去重)

2022-07-19 18:09:17 字數 779 閱讀 2675

現在以下資料

查有出現的teacherid 

1   select  teacherid from student
view code

結果

有重複的id出現

確實查法:

1

select teacherid from student

2 group by teacherid

view code

1 select distinct teacherid from student
view code

結果:

可以看得出  distinct 的作用就是去重

distinct 還可以跟聚合函式使用: 

1   --兩種查法得出的結果一樣 都是統計有多少個不同的老師

2select count( distinct teacherid) from student

34 select count(*) from ( select distinct teacherid from student) t

view code

set容器用法(去重)

關於set,必須說明的是set關聯式容器。set作為乙個容器也是用來儲存同一資料型別的資料型別,並且能從乙個資料集合中取出資料,在set中每個元素的值都唯一,而且系統能根據元素的值自動進行排序。應該注意的是set中數元素的值不能直接被改變。set中常用的方法 begin 返回set容器的第乙個元素 ...

count去重和distinct去重

有兩種去重方法 1 select count distinct id from a 這個是將所有值都放入到記憶體中,key為列值,通過hash算出有多少個key就是多少行。2 select count 1 from select 1 from a group by id a,這個是通過group b...

JS陣列去重,物件去重

例項1根據indexof去重,indexof的好處就是返回的是首次出現的位置,這樣後面即使出現的值一樣,也只能返回第一次出現的索引,當然這個只適用於簡單的陣列 物件陣列去重 const objarr const obj const newobjarr for let i 0 i objarr.len...