博客
关于我
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/

你可能感兴趣的文章
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
NPM使用前设置和升级
查看>>
npm入门,这篇就够了
查看>>
npm切换到淘宝源
查看>>
npm前端包管理工具简介---npm工作笔记001
查看>>
npm发布自己的组件UI包(详细步骤,图文并茂)
查看>>
npm和yarn清理缓存命令
查看>>
npm和yarn的使用对比
查看>>
npm学习(十一)之package-lock.json
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>