
- 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)
廣告