引用:
1、使用distinct去重(適合查詢整張表的總數)
有多個學校+教師投稿,需要統計出作者的總數
select count(author) as total from files
每個作者都投稿很多,這裡有重複的記錄。
select distinct author from files;
有可能兩個學校的教師姓名相同,結果只統計乙個,出錯。
select distinct author,sid from files
統計(作者+學校id)的組合唯一值,結果出現正確的結果,但如何知道一共有多少人呢?
select count(distinct author,sid) as total from files
2、group by 分組去重(適合根據條件分組後查詢每組的總數)
select author, count(distinct id) from files group by sid
3、記錄兩張表的數目的和,這兩個表分開查詢
select sum(c)
from
(select count(distinct from_user_id, message_id) c
from im_message
where dr = 0 and message_status = 2 and user_type = 1 and to_user_id = 2
union all
select count(distinct group_id, message_id) c
from im_messagerefgroup
where dr = 0 and user_id = 2
) as temp ;
mysql技巧之資料去重並記錄總數
1 使用distinct去重 適合查詢整張表的總數 有多個學校 教師投稿,需要統計出作者的總數 select count author as total from files 每個作者都投稿很多,這裡有重複的記錄。select distinct author from files 有可能兩個學校的教...
mysql去重欄位 mysql多字段去重,並計數
問 題 mysql版本5.5.42 有兩個表,表結構與資料如下 1 goods表 create table goods id int 10 unsigned not null,product varchar 180 collate utf8mb4 unicode ci not null,size v...
leetCode39 組合總數 回溯去重記錄
目錄 一 題目描述 二 解題思路 三 實現 給定乙個無重複元素的陣列 candidates 和乙個目標數 target 找出 candidates 中所有可以使數字和為 target 的組合。candidates 中的數字可以無限制重複被選取。說明 示例 1 輸入 candidates 2,3,6,...