博客
关于我
GROUP BY在SQL查询语句中的使用
阅读量:372 次
发布时间:2019-03-04

本文共 859 字,大约阅读时间需要 2 分钟。

表content

"ID" "title" "content" "User_ID" "Type_ID" "Hits" "time" "tag"

 

表type

"ID" "typeName"

 

表users

"ID" "UserName"

 

 

SQL查询语句实现“按某类统计按文章数量给用户排名”

SELECT UserName, aa.ContentCount

FROM [SELECT content.User_ID, Count(content.User_ID) AS ContentCount
FROM content, type
WHERE (((type.ID)=1) AND ((content.Type_ID)=[type].[id]))
GROUP BY content.User_ID
ORDER BY Count(content.User_ID) DESC]. AS aa, Users
WHERE Users.id=aa.User_ID;

 

SQL查询语句实现“统计按文章数量给用户排名”

SELECT UserName, aa.ContentCount

FROM [SELECT content.User_ID, Count(content.User_ID) AS ContentCount
FROM content, type
WHERE (content.Type_ID)=[type].[id]
GROUP BY content.User_ID
ORDER BY Count(content.User_ID) DESC]. AS aa, Users
WHERE Users.id=aa.User_ID;

 

SQL查询语句实现“按用户统计文章点击率排名”

SELECT Sum(content.Hits) AS Hits之总计, content.User_ID

FROM content
GROUP BY content.User_ID
ORDER BY Sum(content.Hits) DESC;

 

 

 

转载地址:http://hsje.baihongyu.com/

你可能感兴趣的文章
MySQL简单查询
查看>>
MySQL管理利器 MySQL Utilities 安装
查看>>
MySQL篇(管理工具)
查看>>
mysql类型转换函数convert与cast的用法
查看>>
mysql系列一
查看>>
MySQL系列之数据类型(Date&Time)
查看>>
MySQL系列之数据类型(Date&Time)
查看>>
Mysql系列之锁机制
查看>>
Mysql系列九:使用zookeeper管理远程Mycat配置文件、Mycat监控、Mycat数据迁移(扩容)...
查看>>
MySql系列:[4200][1140]In aggregated query without GROUP BY, expression #2 of SELECT list contains nona
查看>>
Mysql索引
查看>>
mysql索引
查看>>
mysql索引
查看>>
Mysql索引,索引的优化,如何避免索引失效案例
查看>>
Mysql索引、命令重点介绍
查看>>
mysql索引、索引优化(这一篇包括所有)
查看>>
Mysql索引一篇就够了
查看>>
MySQL索引一篇带你彻底搞懂(一次讲清实现原理加优化实战,面试必问)
查看>>
MySQL索引下沉:提升查询性能的隐藏秘
查看>>
MySql索引为什么使用B+树
查看>>