mysql之数据去重并记录总数
引用:http://blog.sina.com.cn/s/blog_6c9d65a10101bkgk.htmlhttp://www.jb51.net/article/39302.htm 1、使用distinct去重(适合查询整张表的总数)有多个学校+教师投稿,需要统计出作者的总数select count(author) as total from files每个作者都投稿很多,这里有重复的记录。s
引用:
http://blog.sina.com.cn/s/blog_6c9d65a10101bkgk.html
http://www.jb51.net/article/39302.htm
1、使用distinct去重(适合查询整张表的总数)
有多个学校+教师投稿,需要统计出作者的总数
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 ;
更多推荐
所有评论(0)