明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

mysql count distinct 統(tǒng)計(jì)結(jié)果去重

[摘要]1、使用distinct去重(適合查詢整張表的總數(shù))有多個(gè)學(xué)校+教師投稿,需要統(tǒng)計(jì)出作者的總數(shù)select count(author) as total from files每個(gè)作者都投稿很多,這里...
1、使用distinct去重(適合查詢整張表的總數(shù))有多個(gè)學(xué)校+教師投稿,需要統(tǒng)計(jì)出作者的總數(shù)select count(author) as total from files每個(gè)作者都投稿很多,這里有重復(fù)的記錄。select distinct author from files;有可能兩個(gè)學(xué)校的教師姓名相同,結(jié)果只統(tǒng)計(jì)一個(gè),出錯(cuò)。select distinct author,sid from files統(tǒng)計(jì)(作者+學(xué)校id)的組合唯一值,結(jié)果出現(xiàn)正確的結(jié)果,但如何知道一共有多少人呢?select count(distinct author,sid) as total from files


2、group by 分組去重(適合根據(jù)條件分組后查詢每組的總數(shù))

select author, count(distinct id) from files group by sid


3、記錄兩張表的數(shù)目的和,這兩個(gè)表分開查詢

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 count distinct 統(tǒng)計(jì)結(jié)果去重的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!


學(xué)習(xí)教程快速掌握從入門到精通的SQL知識。