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

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

数据库表结构与SQL查询实现

数据库表结构

表content

ID, Title, Content, User_ID, Type_ID, Hits, Time, Tag

表type

ID, TypeName

表users

ID, UserName

SQL查询语句

按某类统计按文章数量给用户排名

SELECT Users.UserName, COUNT(content.User_ID) AS ContentCountFROM content, typeWHERE type.ID = 1 AND content.Type_ID = type.IDGROUP BY content.User_IDORDER BY ContentCount DESC;

统计按文章数量给用户排名

SELECT Users.UserName, COUNT(content.User_ID) AS ContentCountFROM content, typeWHERE content.Type_ID = type.IDGROUP BY content.User_IDORDER BY ContentCount DESC;

按用户统计文章点击率排名

SELECT SUM(content.Hits) AS HitsCount, content.User_IDFROM contentGROUP BY content.User_IDORDER BY HitsCount DESC;

这三个查询分别实现了不同类型的数据统计需求,涵盖了按类别统计、直接统计以及点击率排名等多种场景。通过合理的表连接和分组操作,可以清晰地看到每个用户的具体数据表现。

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

你可能感兴趣的文章
SSM(Spring+SpringMvc+Mybatis)整合开发笔记
查看>>
Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
查看>>
org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
查看>>
sql查询中 查询字段数据类型 int 与 String 出现问题
查看>>
org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
查看>>
org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
查看>>
sqlserver学习笔记(三)—— 为数据库添加新的用户
查看>>
org.apache.http.conn.HttpHostConnectException: Connection to refused
查看>>
org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>