两张表之间进行数据库查询时的聚合函数用法

简介: 注意:表中内容中文显示时有乱码现象,我都用Hello World代替! 【comment(评论表)】:                  Bookid userid comment star ts00001 1 Hello World 4 ts00001 2 H...

注意:表中内容中文显示时有乱码现象,我都用Hello World代替!

comment(评论表)】:

                

Bookid userid comment star
ts00001 1 Hello World 4
ts00001 2 Hello World 6
ts00001 5 Hello My Java 9
ts00002 1 123 4
ts00002 2 123 4
ts00003 2 Hello World 4
ts00004 2 Hello World 4
ts00005 1 Hello World 4
ts00006 2 Hello World 4
ts00007 1 Hello World 4
ts00008 2 Hello World 4
ts00009 1 Hello World 4
ts00010 2 Hello World 4
ts00011 2 safdsa 4
ts00012 1 sdfasd 4

Books(图书表)】:

bookid name author introduce publisher publicationtime inventory images istop categorycode isbn
ts00001 C# Joe Mayo Hello World Hello World 1900-01-01 00:00:00.000 5 ../img/books/ts00001.jpg True TP312 510 12312312312
ts00002 SQL Server []Solid Quality Learning Hello World Hello World 2007-02-01 00:00:00.000 5 ../img/books/ts00002.jpg True TP312 840 9787302163305
ts00003 ASP.NET2.0 []Chris Hart John Kauffman Hello Funs Hello World 1900-01-01 00:00:00.000 1 ../img/books/ts00003.jpg True TP312 830 978730223305
ts00004 C Hello World Hello World Hello World 2010-06-01 00:00:00.000 7 ../img/books/ts00004.jpg True TP312 630 9787302224464
ts00005 C++ Primer Hello WorldStanley Hello World Hello World 2006-03-01 00:00:00.000 5 ../img/books/ts00005.jpg True TP312 820 9787115145543
ts00006 Java []Bruce Eckel Hello World Hello World 2008-12-01 00:00:00.000 6 ../img/books/ts00006.jpg True TP312 510 9787111256113
ts00007 Hello World Hello World Hello World Hello World 2007-12-01 00:00:00.000 5 ../img/books/ts00007.jpg True TP312 130 9787302162063
ts00008 Hello World Hello World Hello World Hello World 2010-01-01 00:00:00.000 5 ../img/books/ts00008.jpg True TP312 220 9787115216878
ts00009 Hello World Hello World Hello World Hello World 2004-02-01 00:00:00.000 4 ../img/books/ts00009.jpg True TP312 110 9787111135104
ts00010 Hello World Hello World Hello World Hello World 2011-01-01 00:00:00.000 4 ../img/books/ts00010.jpg True TP312 150 9787111321330
ts00011 Hello World Hello World Hello World Hello World 2009-08-01 00:00:00.000 3 ../img/books/ts00001.jpg True TP312 500 9787810821698
ts00012 PowerPoint Hello World PowerPoint 2007Hello World Hello World 2009-03-01 00:00:00.000 5 ../img/books/ts00011.jpg True TP312 430 9787122046901

问题是】现在我要查询两张表中comment中(bookid,name,author,introduce images)和books中(star)并且star要返回平均值,最后结果按bookid,name,author,introduce,images分组!

查询语法】:

select top 12 c.bookid,left(b.name,10) as name,left(b.author,10) as author,left(b.introduce,40) as introduce,b.images,avg(c.star) as star from Books b inner join comment c on b.bookid=c.bookid group by c.bookid,name,author,introduce,images having avg(c.star)>=4

结果为】:

bookid name author introduce images star
ts00001 C# Joe Mayo Hello World ../img/books/ts00001.jpg 6
ts00002 SQL Server [美]Solid Q 《SQLServer2005实现与维护(附光盘MCTS教程)》是微软认证技术专家 ../img/books/ts00002.jpg 4
ts00003 ASP.NET2.0 [美]Chris H Hello Funs ../img/books/ts00003.jpg 4
ts00004 C程序设计(第四版) 谭浩强 著 由谭浩强教授著、清华大学出版社出版的《C程序设计》是一本公认的学习C语言程序设计 ../img/books/ts00004.jpg 4
ts00005 C++ Primer :(美)Stanle 作为目前业界广泛使用的编程语言,C++可谓包罗万象、博大精深。20年来,讲述C+ ../img/books/ts00005.jpg 4
ts00006 Java编程思想 [美]Bruce E 本书赢得了全球程序员的广泛赞誉,即使是最晦涩的概念,在Bruce Eckel的文 ../img/books/ts00006.jpg 4
ts00007 大话设计模式 程杰 本书通篇都是以情景对话的形式,用多个小故事或编程示例来组织讲解GoF(设计模式的 ../img/books/ts00007.jpg 4
ts00008 代码整洁之道 (美)马丁 作者Martin是软件工程领域的大师级人物,是《敏捷软件开发:原则、模式与实践》 ../img/books/ts00008.jpg 4
ts00009 计算机程序的构造和解 :[美]艾伯森 这一版本中强调了几个新问题,其中最重要的是有关的不同的途径中,计算模型里对于时间 ../img/books/ts00009.jpg 4
ts00010 深入理解计算机系统( (美)布莱恩特,奥哈 本书从程序员的视角详细阐述计算机系统的本质概念,并展示这些概念如何实实在在地影响 ../img/books/ts00010.jpg 4
ts00011 单片机原理与实用技术 付晓光 本书以人们对新知识的认识过程为顺序,从分析单片机的应用实例出发,以MCS-51系 ../img/books/ts00001.jpg 4
ts00012 PowerPoint 墨思客工作室 PowerPoint 2007是Microsoft公司推出的Office 200 ../img/books/ts00011.jpg 4

 

 【总结:】

1、select 子句只能包含集合函数和出现在Group by子句中的分组列

2、集合函数只能应用于select子句和Having子句

3、使用Having子句之前必须使用Group by子句对数据行分组

4、Having子句中的条件表达式必须使用集合函数来构造

 【关键点】:

千万不要在group by 子句中加star,因为加了以后,查询就不会返回平均值,而是返回分组,千万注意!

 

目录
相关文章
|
人工智能 安全 机器人
无代码革命:10分钟打造企业专属数据库查询AI机器人
随着数字化转型加速,企业对高效智能交互解决方案的需求日益增长。阿里云AppFlow推出的AI助手产品,借助创新网页集成技术,助力企业打造专业数据库查询助手。本文详细介绍通过三步流程将AI助手转化为数据库交互工具的核心优势与操作指南,包括全场景适配、智能渲染引擎及零代码配置等三大技术突破。同时提供Web集成与企业微信集成方案,帮助企业实现便捷部署与安全管理,提升内外部用户体验。
1236 12
无代码革命:10分钟打造企业专属数据库查询AI机器人
|
Cloud Native 关系型数据库 分布式数据库
|
并行计算 关系型数据库 MySQL
如何用 esProc 将数据库表转储提速查询
当数据库查询因数据量大或繁忙变慢时,可借助 esProc 将数据导出为文件进行计算,大幅提升性能。以 MySQL 的 3000 万行订单数据为例,两个典型查询分别耗时 17.69s 和 63.22s。使用 esProc 转储为二进制行存文件 (btx) 或列存文件 (ctx),结合游标过滤与并行计算,性能显著提升。例如,ctx 并行计算将原查询时间缩短至 0.566s,TopN 运算提速达 30 倍。esProc 的简洁语法和高效文件格式,特别适合历史数据的复杂分析场景。
|
SQL 关系型数据库 MySQL
如何优化SQL查询以提高数据库性能?
这篇文章以生动的比喻介绍了优化SQL查询的重要性及方法。它首先将未优化的SQL查询比作在自助餐厅贪多嚼不烂的行为,强调了只获取必要数据的必要性。接着,文章详细讲解了四种优化策略:**精简选择**(避免使用`SELECT *`)、**专业筛选**(利用`WHERE`缩小范围)、**高效联接**(索引和限制数据量)以及**使用索引**(加速搜索)。此外,还探讨了如何避免N+1查询问题、使用分页限制结果、理解执行计划以及定期维护数据库健康。通过这些技巧,可以显著提升数据库性能,让查询更高效流畅。
|
数据库
【YashanDB知识库】数据库用户所拥有的权限查询
【YashanDB知识库】数据库用户所拥有的权限查询
|
存储 运维 监控
百万指标,秒级查询,零宕机——时序数据库 TDengine 在 AIOps 中的硬核实战
本篇文章详细讲述了七云团队在运维平台中如何利用 TDengine 解决海量时序数据存储与查询的实际业务需求。内容涵盖了从数据库选型、方案落地到业务挑战及解决办法的完整过程,特别是分享了升级 TDengine 3.x 时的实战经验,给到有需要的小伙伴参考阅读。
867 1
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
673 158
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
12月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1783 152