- Trending Categories
数据结构
网络
RDBMS
操作系统
Java
MS Excel
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP物理
化学
生物
数学
英语
经济
心理学
社会研究
时尚研究
法律研究
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
找出 average 并显示重复 id 的最大平均值?
为此,请使用 AVG()。要找到最大平均值,请使用 MAX() 并按 id 分组。让我们先来创建一个表 -
mysql> create table DemoTable -> ( -> PlayerId int, -> PlayerScore int -> ); Query OK, 0 rows affected (0.55 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values(1,78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(2,82); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values(1,45); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(3,97); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(2,79); Query OK, 1 row affected (0.12 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将生成以下输出 -
+----------+-------------+ | PlayerId | PlayerScore | +----------+-------------+ | 1 | 78 | | 2 | 82 | | 1 | 45 | | 3 | 97 | | 2 | 79 | +----------+-------------+ 5 rows in set (0.00 sec)
这是在 MySQL 中查找重复 id 的最大平均值所需的查询 -
mysql> select PlayerId from DemoTable -> group by PlayerId -> having avg(PlayerScore) > 80;
这将生成以下输出 -
+----------+ | PlayerId | +----------+ | 2 | | 3 | +----------+ 2 rows in set (0.00 sec)
广告