Home > Database > Mysql Tutorial > body text

Detailed explanation of examples of maximum and minimum values, sum query and count query in mysql

黄舟
Release: 2017-09-07 11:10:07
Original
3683 people have browsed it

1. Maximum value query:

mysql> select max(score) from 4a;
+------------+
| max(score) |
+------------+
|         93 |
+------------+
1 row in set (0.06 sec)
Copy after login

2. Minimum value query:

mysql> select max(4a.score),min(4inall.score) from 4a,4inall;
+---------------+-------------------+
| max(4a.score) | min(4inall.score) |
+---------------+-------------------+
|            93 |                35 |
+---------------+-------------------+
1 row in set (0.08 sec)
Copy after login

3. Sum query and count query:

This is the original table, next The statements are all written based on this table.

mysql> select * from 4a;
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 刘一   |   18 | 叶平   |    1 | 语文   |    1 |    1 |    56 |
| 刘一   |   18 | 贺高   |    2 | 数学   |    1 |    2 |    78 |
| 刘一   |   18 | 杨艳   |    3 | 英语   |    1 |    3 |    67 |
| 刘一   |   18 | 周磊   |    4 | 物理   |    1 |    4 |    58 |
| 钱二   |   19 | 叶平   |    1 | 语文   |    2 |    1 |    79 |
| 钱二   |   19 | 贺高   |    2 | 数学   |    2 |    2 |    81 |
| 钱二   |   19 | 杨艳   |    3 | 英语   |    2 |    3 |    92 |
| 钱二   |   19 | 周磊   |    4 | 物理   |    2 |    4 |    68 |
| 张三   |   17 | 叶平   |    1 | 语文   |    3 |    1 |    91 |
| 张三   |   17 | 贺高   |    2 | 数学   |    3 |    2 |    47 |
| 张三   |   17 | 杨艳   |    3 | 英语   |    3 |    3 |    88 |
| 张三   |   17 | 周磊   |    4 | 物理   |    3 |    4 |    56 |
| 李四   |   18 | 贺高   |    2 | 数学   |    4 |    2 |    88 |
| 李四   |   18 | 杨艳   |    3 | 英语   |    4 |    3 |    90 |
| 李四   |   18 | 周磊   |    4 | 物理   |    4 |    4 |    93 |
| 王五   |   17 | 叶平   |    1 | 语文   |    5 |    1 |    46 |
| 王五   |   17 | 杨艳   |    3 | 英语   |    5 |    3 |    78 |
| 王五   |   17 | 周磊   |    4 | 物理   |    5 |    4 |    53 |
| 赵六   |   19 | 叶平   |    1 | 语文   |    6 |    1 |    35 |
| 赵六   |   19 | 贺高   |    2 | 数学   |    6 |    2 |    68 |
| 赵六   |   19 | 周磊   |    4 | 物理   |    6 |    4 |    71 |
| NULL   | NULL | NULL   | NULL | NULL   | NULL | NULL |    93 |
+--------+------+--------+------+--------+------+------+-------+
22 rows in set (0.00 sec)
Copy after login
mysql> select sum(sname) from 4a;
+------------+
| sum(sname) |
+------------+
|          0 |
+------------+
1 row in set, 21 warnings (0.00 sec)
Copy after login

We can see that although SNAME has many records, if you use SUM to query its sum, although there is no syntax error, the result is obviously "wrong".

mysql> select count(sname) from 4a;
+--------------+
| count(sname) |
+--------------+
|           21 |
+--------------+
1 row in set (0.00 sec)
Copy after login

There is no problem in using count to count SNAME. Of course, NULL records will not be treated as a countable record.

mysql> select count(score) from 4a;
+--------------+
| count(score) |
+--------------+
|           22 |
+--------------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of Detailed explanation of examples of maximum and minimum values, sum query and count query in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template