MySQLの算術式、集約関数およびGROUP BYとHANVINGなどの関数の応用

6354 ワード

一、MySQLの算術式
算術式は加減乗除の演算過程であり、主に1つのデータに現れた数字を統計し、演算する.
まず、次のようなデータテーブルがあります.
mysql> select * from test_score;
+----+-------+--------+-------+--------+--------+
| id | class | name   | yuwen | shuxue | yingyu |
+----+-------+--------+-------+--------+--------+
|  1 |     1 |       |    95 |     90 |     97 |
|  2 |     1 |       |    59 |     88 |     90 |
|  3 |     1 |       |    99 |     99 |     99 |
|  4 |     1 |       |    51 |     56 |     59 |
|  5 |     2 |       |    92 |     94 |     93 |
|  6 |     2 |       |    92 |     97 |     89 |
|  7 |     2 |       |    59 |     60 |     61 |
|  8 |     2 |       |    55 |     50 |     54 |
|  9 |     3 |       |    53 |     54 |     51 |
| 10 |     3 |       |    49 |     47 |     54 |
| 11 |     3 |       |    47 |     51 |     50 |
+----+-------+--------+-------+--------+--------+
11 rows in set (0.00 sec)

合計スコアは次のように集計されます.
#         
mysql> SELECT (yuwen+shuxue+yingyu) AS total FROM test_score where name='  ';
+-------+
| total |
+-------+
|   282 |
+-------+
1 row in set (0.00 sec)

二、SUM()関数の使用
定義:SUM()関数を使用して、値または式のセットの合計を計算します.以前、学校に通っていたとき、先生は各科の平均点を統計して、誰が足を引っ張ったかを見ました.まず国語を計算するのは、まずクラスの国語の得点を合計して総得点を得て、それから学生の人数を除いて、平均点を得て、平均点を下回る学生は運が悪いです.
#           
mysql> SELECT sum(yuwen) AS yuwen_total FROM test_score;
+-------------+
| yuwen_total |
+-------------+
|         751 |
+-------------+
1 row in set (0.00 sec)

#    
mysql> SELECT count(*) AS total FROM test_score;
+-------+
| total |
+-------+
|    11 |
+-------+
1 row in set (0.00 sec)

#         
mysql> select sum(yuwen)/count(*) from test_score;
+---------------------+
| sum(yuwen)/count(*) |
+---------------------+
|             68.2727 |
+---------------------+
1 row in set (0.04 sec)

三、集約関数
MySQLは、AVG、COUNT、SUM、MIN、MAXなど、多くの集約関数を提供しています.COUNT関数以外の集約関数は、計算時にNULL値を無視します.
#     
mysql> SELECT MAX(yuwen) FROM test_score;
+------------+
| MAX(yuwen) |
+------------+
|         99 |
+------------+
1 row in set (0.00 sec)

#          
mysql> SELECT class,MIN(yuwen) FROM test_score;
+-------+------------+
| class | MIN(yuwen) |
+-------+------------+
|     1 |         47 |
+-------+------------+
1 row in set (0.00 sec)

#               
mysql> SELECT avg(yuwen) AS total FROM test_score;
+---------+
| total   |
+---------+
| 68.2727 |
+---------+
1 row in set (0.00 sec)

#            
mysql> SELECT format(avg(yuwen),2) AS total FROM test_score;
+-------+
| total |
+-------+
| 68.27 |
+-------+
1 row in set (0.00 sec)

#              
mysql> SELECT avg(DISTINCT yuwen) AS total FROM test_score;
+---------+
| total   |
+---------+
| 66.6667 |
+---------+
1 row in set (0.00 sec)

四、GROUP BYとHAVINGの応用
#GROUP BY  
#       
mysql> SELECT class,sum(yuwen) AS yuwen_total   FROM test_score GROUP BY class;
+-------+-------------+
| class | yuwen_total |
+-------+-------------+
|     1 |         304 |
|     2 |         298 |
|     3 |         149 |
+-------+-------------+
3 rows in set (0.00 sec)

#     
mysql> SELECT class,count(*) AS total FROM test_score GROUP BY class;
+-------+-------+
| class | total |
+-------+-------+
|     1 |     4 |
|     2 |     4 |
|     3 |     3 |
+-------+-------+
3 rows in set (0.00 sec)

#           
mysql> SELECT class,avg(yuwen) AS total FROM test_score GROUP BY class;
+-------+---------+
| class | total   |
+-------+---------+
|     1 | 76.0000 |
|     2 | 74.5000 |
|     3 | 49.6667 |
+-------+---------+
3 rows in set (0.00 sec)

#        
mysql> SELECT class,MAX(yuwen) FROM test_score GROUP BY class;
+-------+------------+
| class | MAX(yuwen) |
+-------+------------+
|     1 |         99 |
|     2 |         92 |
|     3 |         53 |
+-------+------------+
3 rows in set (0.00 sec)

#     (   )
mysql> SELECT class,MIN(yuwen) AS yuwen FROM test_score GROUP BY class ORDER BY yuwen asc;
+-------+-------+
| class | yuwen |
+-------+-------+
|     3 |    47 |
|     1 |    51 |
|     2 |    55 |
+-------+-------+
3 rows in set (0.00 sec)

#            
mysql> SELECT class,MIN(yuwen) FROM test_score WHERE yuwen<60 GROUP BY class;
+-------+------------+
| class | MIN(yuwen) |
+-------+------------+
|     1 |         51 |
|     2 |         55 |
|     3 |         47 |
+-------+------------+
3 rows in set (0.00 sec)

#             
mysql> SELECT class,avg(yuwen) as yuwen FROM test_score GROUP BY class HAVING yuwen < 60;
+-------+---------+
| class | yuwen   |
+-------+---------+
|     3 | 49.6667 |
+-------+---------+
1 row in set (0.00 sec)