MySQL —— 基本查询

news2024/9/24 6:11:34

文章目录

    • 1. 向表中插入数据
    • 2. 查询操作
      • 2.1 全列查询
      • 2.2 指定列查询
      • 2.3 查询字段带表达式
      • 2.4 为查询结果指定别名
      • 2.5 去重操作
    • 3. where 条件
      • 3.1 比较运算符和逻辑预算符的运用
      • 3.2 like的细节
      • 3. 3 null查询
    • 4. 对查询的结果进行排序
      • 4.1 对单一字段进行排序
      • 4.2 对多个字段排序
      • 4.3 对字段排序结果进行分页
    • 5. 更改表中的数据
      • 5.1 更新单列数据
      • 5.2 更新多列的数据
    • 6. 删除表中数据
      • 6.1 删除表中一个数据或或者多个数据
      • 6.2 删除全表的数据
      • 6.3 截断表,不可以恢复的删除
    • 7. 去重操作
    • 8. 聚合函数

前言: 本文主要讲解MySQL中对表进行一些基本的查询操作,同时在讲一些对表的其他操作,所谓其他操作就是之前博客里面没有涉及到的。


1. 向表中插入数据

向表中插入数据再补充一下,如果插入的数据和主键或者唯一键冲突了的情况。

  1. 先创建
mysql> create table students (id int unsigned primary key auto_increment, sn int not null unique comment '学号',  name varchar(20) not null, qq varchar(20))character set utf8;
Query OK, 0 rows affected (0.04 sec)

mysql> desc students;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| sn    | int(11)          | NO   | UNI | NULL    |                |
| name  | varchar(20)      | NO   |     | NULL    |                |
| qq    | varchar(20)      | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
  1. 向表中插入数据,单行全列插入:
mysql> insert into students values(1,100,"张三",22222);
Query OK, 1 row affected (0.01 sec)

mysql> insert into students values(2,101,"李四",33333);
Query OK, 1 row affected (0.00 sec)

mysql> select * from students;
+----+-----+--------+-------+
| id | sn  | name   | qq    |
+----+-----+--------+-------+
|  1 | 100 | 张三   | 22222 |
|  2 | 101 | 李四   | 33333 |
+----+-----+--------+-------+
2 rows in set (0.00 sec)
  1. 再向表中插入数据,多行全列插入:
mysql> insert into students values(11,111,"王五",4444),(12,222,"刘六",55555);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from students;
+----+-----+--------+-------+
| id | sn  | name   | qq    |
+----+-----+--------+-------+
|  1 | 100 | 张三   | 22222 |
|  2 | 101 | 李四   | 33333 |
| 11 | 111 | 王五   | 4444  |
| 12 | 222 | 刘六   | 55555 |
+----+-----+--------+-------+
4 rows in set (0.00 sec)

这里需要注意的就是,多行插入时,() () 之间用隔开。

  1. 当然还可以指定列插入,现在来演示插入和主键,唯一键冲突了的情况:
mysql> insert into students values(2,101,"开心",33333);
ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY'

很明显这是主键冲突,但是我如果就是想要更新一下name信息,但是发生冲突,导致无法完成插入操作,这就需要这样插入:

INSERT ... ON DUPLICATE KEY UPDATE column = value [, column = value] ...

mysql> insert into students values(2,101,"开心",33333) on duplicate key update name="开心";
Query OK, 2 rows affected (0.01 sec)

mysql> select * from students;
+----+-----+--------+-------+
| id | sn  | name   | qq    |
+----+-----+--------+-------+
|  1 | 100 | 张三   | 22222 |
|  2 | 101 | 开心   | 33333 |
| 11 | 111 | 王五   | 4444  |
| 12 | 222 | 刘六   | 55555 |
+----+-----+--------+-------+
4 rows in set (0.00 sec)

主键冲突,想要更新其余字段就是这样操作。唯一键冲突,和上面一样的操作。这里需要注意一点,那就是主键冲突,更新的字段不能包括主键,唯一键冲突,更新的字段不能包括唯一键。这是因为你更新了,就相当于不冲突了,那就可以直接插入了,而不是利用上面的更新操作。

  1. 上面发生冲突后,采取的是更新的方式,其实还有一种方式,那就是替换。

replace into students (sn, name) values (111, '王五');

  • 如果发生了键冲突,那么就删除原来的,直接插入新的。这就是替换。
  • 如果没有发生键冲突,就是直接插入。

2. 查询操作

我们先创建一个表,然后进行一些查询操作:

mysql> create table exam(id int unsigned primary key auto_increment, name varchar(20) not null comment '同学姓名',  chinese float deefault 0.0 comment'语文成绩',  math float default 0.0 comment'数学成绩',  english float default 0.0 comment'英语成绩');
Query OK, 0 rows affected (0.05 sec)

mysql> desc exam;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20)      | NO   |     | NULL    |                |
| chinese | float            | YES  |     | 0       |                |
| math    | float            | YES  |     | 0       |                |
| english | float            | YES  |     | 0       |                |
+---------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

插入一批数据:

mysql> insert into exam values(1,'bob',88.5,75.4,98.5),(2,'xm',88,76,93),(3,'gg',76,77,98);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into exam values(4,'ss',82.5,72.4,98.5),(5,'ll',88,74,97),(6,'ff',75,72,93);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

2.1 全列查询

select * from 表名

全列查询,这个就是咱们实验用的比较多,一般情况下,很少用,因为全列显示,意味着需要传输的数据量越大,可能会影响到索引的使用。

比如:

mysql> select * from exam;
+----+------+---------+------+---------+
| id | name | chinese | math | english |
+----+------+---------+------+---------+
|  1 | bob  |    88.5 | 75.4 |    98.5 |
|  2 | xm   |      88 |   76 |      93 |
|  3 | gg   |      76 |   77 |      98 |
|  4 | ss   |    82.5 | 72.4 |    98.5 |
|  5 | ll   |      88 |   74 |      97 |
|  6 | ff   |      75 |   72 |      93 |
+----+------+---------+------+---------+
6 rows in set (0.00 sec)

2.2 指定列查询

就是指定部分列进行查询,不是全列查询,需要指定:

select 指定列 from exam:

mysql> select id,name,math from exam;
+----+------+------+
| id | name | math |
+----+------+------+
|  1 | bob  | 75.4 |
|  2 | xm   |   76 |
|  3 | gg   |   77 |
|  4 | ss   | 72.4 |
|  5 | ll   |   74 |
|  6 | ff   |   72 |
+----+------+------+
6 rows in set (0.00 sec)

2.3 查询字段带表达式

比如:现在要求 显示出来的math都加10:

select id,name,math+10 from exam:

mysql> select id,name,math+10 from exam;
+----+------+------------------+
| id | name | math+10          |
+----+------+------------------+
|  1 | bob  | 85.4000015258789 |
|  2 | xm   |               86 |
|  3 | gg   |               87 |
|  4 | ss   | 82.4000015258789 |
|  5 | ll   |               84 |
|  6 | ff   |               82 |
+----+------+------------------+

注意这个+10,只影响显示结果,不会影响 表中的原始数据。

现在要求显示出总分:

mysql> select id,name,math+chinese+english from exam;
+----+------+----------------------+
| id | name | math+chinese+english |
+----+------+----------------------+
|  1 | bob  |    262.4000015258789 |
|  2 | xm   |                  257 |
|  3 | gg   |                  251 |
|  4 | ss   |    253.4000015258789 |
|  5 | ll   |                  259 |
|  6 | ff   |                  240 |
+----+------+----------------------+

2.4 为查询结果指定别名

把上面的math+chinese+english,起别名为总分:

mysql> select id,name,math+chinese+english 总分 from exam ;
+----+------+-------------------+
| id | name | 总分              |
+----+------+-------------------+
|  1 | bob  | 262.4000015258789 |
|  2 | xm   |               257 |
|  3 | gg   |               251 |
|  4 | ss   | 253.4000015258789 |
|  5 | ll   |               259 |
|  6 | ff   |               240 |
+----+------+-------------------+
6 rows in set (0.00 sec)

2.5 去重操作

select distinct 字段名 from 表名;

mysql> select chinese from exam;
+---------+
| chinese |
+---------+
|    88.5 |
|      88 |
|      76 |
|    82.5 |
|      88 |
|      75 |
+---------+

可以看到里面有重复的情况,那就是88和88重复了,那么去重就是:

mysql> select distinct chinese from exam;
+---------+
| chinese |
+---------+
|    88.5 |
|      88 |
|      76 |
|    82.5 |
|      75 |
+---------+

3. where 条件

比较运算符:

在这里插入图片描述
逻辑运算符:
在这里插入图片描述

3.1 比较运算符和逻辑预算符的运用

假如:我要语文成绩在[80,90]之间的同学名单:

mysql> select name,chinese from exam where chinese>=80 and chinese<=90;
+------+---------+
| name | chinese |
+------+---------+
| bob  |    88.5 |
| xm   |      88 |
| ss   |    82.5 |
| ll   |      88 |
+------+---------+
4 rows in set (0.00 sec)

先在要求: 数学成绩大于75或者语文成绩大于85:

mysql> select name,chinese,math from exam where math>75 or chinese>=85;
+------+---------+------+
| name | chinese | math |
+------+---------+------+
| bob  |    88.5 | 75.4 |
| xm   |      88 |   76 |
| gg   |      76 |   77 |
| ll   |      88 |   74 |
+------+---------+------+
4 rows in set (0.00 sec)

3.2 like的细节

找出 名字为bob的同学:

mysql> select * from exam where name like 'bob';
+----+------+---------+------+---------+
| id | name | chinese | math | english |
+----+------+---------+------+---------+
|  1 | bob  |    88.5 | 75.4 |    98.5 |
+----+------+---------+------+---------+
1 row in set (0.00 sec)

其实这里也可以用 name = ‘bob’ ,不过这里用like 。

关于like的两个细节:%,_。

比如我要搜索 b后面只有一个字母的人,bb之类的。如果不限制后面字母数 那就是 %:

mysql> select * from exam where name like 'b_';
+----+------+---------+------+---------+
| id | name | chinese | math | english |
+----+------+---------+------+---------+
|  7 | bb   |      87 |   86 |      76 |
+----+------+---------+------+---------+
1 row in set (0.00 sec)

mysql> select * from exam where name like 'b%';
+----+------+---------+------+---------+
| id | name | chinese | math | english |
+----+------+---------+------+---------+
|  1 | bob  |    88.5 | 75.4 |    98.5 |
|  7 | bb   |      87 |   86 |      76 |
+----+------+---------+------+---------+
2 rows in set (0.00 sec)

3. 3 null查询

  • is null
  • is not null

null不参与一般的运算,如果要让它参与比较运算,也是可以的,那就是用 is null和is not null,这样好用。如果非要用比较运算符的话,那就是<=>等于。

mysql> select * from students;
+----+-----+--------+-------+
| id | sn  | name   | qq    |
+----+-----+--------+-------+
|  3 | 102 | 开学   | 33333 |
| 11 | 111 | 王五   | 4444  |
| 12 | 222 | 刘六   | 55555 |
| 13 | 100 | 发财   | NULL  |
+----+-----+--------+-------+

比如:上面那个表,其中发财的qq是空。

mysql> select * from students where qq is not null;
+----+-----+--------+-------+
| id | sn  | name   | qq    |
+----+-----+--------+-------+
|  3 | 102 | 开学   | 33333 |
| 11 | 111 | 王五   | 4444  |
| 12 | 222 | 刘六   | 55555 |
+----+-----+--------+-------+
3 rows in set (0.00 sec)

mysql> select * from students where qq is null;
+----+-----+--------+------+
| id | sn  | name   | qq   |
+----+-----+--------+------+
| 13 | 100 | 发财   | NULL |
+----+-----+--------+------+
1 row in set (0.00 sec)

mysql> select * from students where qq <=> null;
+----+-----+--------+------+
| id | sn  | name   | qq   |
+----+-----+--------+------+
| 13 | 100 | 发财   | NULL |
+----+-----+--------+------+
1 row in set (0.00 sec)

4. 对查询的结果进行排序

-- asc 为升序(从小到大)
-- desc 为降序(从大到小)
-- 默认为 asc

4.1 对单一字段进行排序

假如:对数学成绩进行排序:

mysql> select id,name,math  from exam order by math;
+----+------+------+
| id | name | math |
+----+------+------+
|  6 | ff   |   72 |
|  4 | ss   | 72.4 |
|  5 | ll   |   74 |
|  1 | bob  | 75.4 |
|  2 | xm   |   76 |
|  3 | gg   |   77 |
|  7 | bb   |   86 |
+----+------+------+
7 rows in set (0.00 sec)

对吧,很明显是升序,也就是默认asc。

那么如果要排降序呢,那就是选desc:

mysql> select id,name,math  from exam order by math desc;
+----+------+------+
| id | name | math |
+----+------+------+
|  7 | bb   |   86 |
|  3 | gg   |   77 |
|  2 | xm   |   76 |
|  1 | bob  | 75.4 |
|  5 | ll   |   74 |
|  4 | ss   | 72.4 |
|  6 | ff   |   72 |
+----+------+------+
7 rows in set (0.00 sec)


4.2 对多个字段排序

对多个字段进行排序:
查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示:

注意其实多字段排序是不能完成的,你想想吧,有的数学高,有的英语高,这不能在一张表中排序,所以多字段排序其实就是你写的要排序的第一个字段:

mysql> select * from exam order by math desc,english,chinese;
+----+------+---------+------+---------+
| id | name | chinese | math | english |
+----+------+---------+------+---------+
|  7 | bb   |      87 |   86 |      76 |
|  3 | gg   |      76 |   77 |      98 |
|  2 | xm   |      88 |   76 |      93 |
|  1 | bob  |    88.5 | 75.4 |    98.5 |
|  5 | ll   |      88 |   74 |      97 |
|  4 | ss   |    82.5 | 72.4 |    98.5 |
|  6 | ff   |      75 |   72 |      93 |
+----+------+---------+------+---------+
7 rows in set (0.00 sec)

这只对math进行了排序。

4.3 对字段排序结果进行分页

起始下标为 0
-- 从 0 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- 从 s 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;

比如:我只想看一些,数学的第一名:

mysql> select id,name,math from exam order by math desc limit 1 offset 0;
+----+------+------+
| id | name | math |
+----+------+------+
|  7 | bb   |   86 |
+----+------+------+
1 row in set (0.00 sec)

我想看数学的前三名:

mysql> select id,name,math from exam order by math desc limit 3 offset 0;
+----+------+------+
| id | name | math |
+----+------+------+
|  7 | bb   |   86 |
|  3 | gg   |   77 |
|  2 | xm   |   76 |
+----+------+------+
3 rows in set (0.00 sec)

我想看从[4,6]名:

mysql> select id,name,math from exam order by math desc limit 3 offset 3;
+----+------+------+
| id | name | math |
+----+------+------+
|  1 | bob  | 75.4 |
|  5 | ll   |   74 |
|  4 | ss   | 72.4 |
+----+------+------+
3 rows in set (0.00 sec)

5. 更改表中的数据

UPDATE table_name SET column = expr [, column = expr ...][WHERE ...] [ORDER BY ...] [LIMIT ...]

5.1 更新单列数据

比如我想用更改bob的语文成绩:

mysql> select name,chinese from exam where name like'bob';
+------+---------+
| name | chinese |
+------+---------+
| bob  |    88.5 |
+------+---------+
1 row in set (0.00 sec)

mysql> update exam  set chinese=90 where name like'bob';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select name,chinese from exam where name like'bob';
+------+---------+
| name | chinese |
+------+---------+
| bob  |      90 |
+------+---------+
1 row in set (0.00 sec)

mysql> 

5.2 更新多列的数据

假如我要更新bob的语文和数学成绩:

mysql> update exam  set chinese=84,math=90 where name like'bob';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select name,chinese,math from exam where name like'bob';
+------+---------+------+
| name | chinese | math |
+------+---------+------+
| bob  |      84 |   90 |
+------+---------+------+
1 row in set (0.00 sec)

更新表中数据,如果不加上 where条件句的限制,那就是全列更新。但是全列更新这个操作用的不多,要慎用。update操作当然不仅是简单的修改值,还可以:比如说要把某个人的语文成绩+30,update exam set chinese=chinese+30 where name like'bob';或者说语文成绩变成原来的2倍,update exam set chinese=chinese*2 where name like'bob。像上面的操作都是可以的,不过不支持什么 += ,*= 等复合操作。


6. 删除表中数据

6.1 删除表中一个数据或或者多个数据

要求删除bob的成绩:

mysql> select * from exam;
+----+------+---------+------+---------+
| id | name | chinese | math | english |
+----+------+---------+------+---------+
|  1 | bob  |      84 |   90 |    98.5 |
|  2 | xm   |      88 |   76 |      93 |
|  3 | gg   |      76 |   77 |      98 |
|  4 | ss   |    82.5 | 72.4 |    98.5 |
|  5 | ll   |      88 |   74 |      97 |
|  6 | ff   |      75 |   72 |      93 |
|  7 | bb   |      87 |   86 |      76 |
+----+------+---------+------+---------+
7 rows in set (0.00 sec)

mysql> delete from exam where name='bob';
Query OK, 1 row affected (0.02 sec)

mysql> select * from exam;
+----+------+---------+------+---------+
| id | name | chinese | math | english |
+----+------+---------+------+---------+
|  2 | xm   |      88 |   76 |      93 |
|  3 | gg   |      76 |   77 |      98 |
|  4 | ss   |    82.5 | 72.4 |    98.5 |
|  5 | ll   |      88 |   74 |      97 |
|  6 | ff   |      75 |   72 |      93 |
|  7 | bb   |      87 |   86 |      76 |
+----+------+---------+------+---------+
6 rows in set (0.01 sec)

我现在要求删除xm和gg的成绩:

mysql> delete from exam where name='xm' or  name='gg';
Query OK, 2 rows affected (0.00 sec)

mysql> select * from exam;
+----+------+---------+------+---------+
| id | name | chinese | math | english |
+----+------+---------+------+---------+
|  4 | ss   |    82.5 | 72.4 |    98.5 |
|  5 | ll   |      88 |   74 |      97 |
|  6 | ff   |      75 |   72 |      93 |
|  7 | bb   |      87 |   86 |      76 |
+----+------+---------+------+---------+
4 rows in set (0.00 sec)

6.2 删除全表的数据

这个操作要慎用:

delete from 表名

这个操作就不演示了,不过会在下面进行讲解。

6.3 截断表,不可以恢复的删除

像是上面的delete操作,之后都是可以恢复数据的,这个可以通过MySQL的日志功能进行恢复。但是truncate操作之后,是不能恢复数据的。

我创建一个表test,专门用于实验这点:

mysql> CREATE TABLE for_delete (
    -> id INT PRIMARY KEY AUTO_INCREMENT,
    -> name VARCHAR(20));
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from for_delete;
+----+------+
| id | name |
+----+------+
|  1 | A    |
|  2 | B    |
|  3 | C    |
+----+------+
3 rows in set (0.00 sec)

mysql> delete from for_delete;
Query OK, 3 rows affected (0.01 sec)

mysql> select * from for_delete;
Empty set (0.00 sec)

mysql> insert into for_delete(name) values('W');
Query OK, 1 row affected (0.00 sec)

mysql> select * from for_delete;
+----+------+
| id | name |
+----+------+
|  4 | W    |
+----+------+
1 row in set (0.00 sec)

mysql> 

可以看到,delete全表后,继续插入数据,自增的id 是有记录的,它自动从id=3后进行增加。为什么?我们来查看表结构:

在这里插入图片描述
这里是有记录的,说明 下次 自增的话,id就会是5。

那么我们再来实验一下,truncate操作:

mysql> CREATE TABLE for_truncate( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20));
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO for_truncate (name) VALUES ('A'), ('B'), ('C');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from for_truncate;
+----+------+
| id | name |
+----+------+
|  1 | A    |
|  2 | B    |
|  3 | C    |
+----+------+
3 rows in set (0.00 sec)

mysql> truncate for_truncate;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from for_truncate;
Empty set (0.00 sec)

mysql> show create table for_truncate\G;
*************************** 1. row ***************************
       Table: for_truncate
Create Table: CREATE TABLE `for_truncate` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> insert into for_truncate (name) values('W');
Query OK, 1 row affected (0.02 sec)

mysql> select * from for_truncate;
+----+------+
| id | name |
+----+------+
|  1 | W    |
+----+------+
1 row in set (0.00 sec)

mysql> 

从这里就能看到,truncate是没有保留以前的自增长的。

7. 去重操作

我创建一个新的表,演示去重操作:

mysql> create table test (id int,name varchar(20));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test values(1,'bb'),(2,'yy'),(3,'bb'),(1,'bb');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from test;
+------+------+
| id   | name |
+------+------+
|    1 | bb   |
|    2 | yy   |
|    3 | bb   |
|    1 | bb   |
+------+------+
4 rows in set (0.00 sec)

mysql> select distinct * from test ;
+------+------+
| id   | name |
+------+------+
|    1 | bb   |
|    2 | yy   |
|    3 | bb   |
+------+------+
3 rows in set (0.00 sec)

用distinct修饰字段,就是去重操作。

mysql> select distinct name from test ;
+------+
| name |
+------+
| bb   |
| yy   |
+------+

8. 聚合函数

  • count : 返回查询到的数据的数量总和
  • sum: 返回查询到的数据的总和
  • avg:返回查询到的数据的平均值
  • max:返回查询到的数据的最大值
  • min: 返回查询到的数据的最小值

注意:null不会加入聚合函数的计算。

比如:

我想看一下表中有多少数据:

mysql> select * from students;
+----+-----+--------+-------+
| id | sn  | name   | qq    |
+----+-----+--------+-------+
|  3 | 102 | 开学   | 33333 |
| 11 | 111 | 王五   | 4444  |
| 12 | 222 | 刘六   | 55555 |
| 13 | 100 | 发财   | NULL  |
+----+-----+--------+-------+
4 rows in set (0.00 sec)

mysql> select count(*) from students;
+----------+
| count(*) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)

mysql> select count(id) from students;
+-----------+
| count(id) |
+-----------+
|         4 |
+-----------+
1 row in set (0.00 sec

聚合函数中的sum使用,sum就是给一列进行求和:

mysql> select * from exam;
+----+------+---------+------+---------+
| id | name | chinese | math | english |
+----+------+---------+------+---------+
|  4 | ss   |    82.5 | 72.4 |    98.5 |
|  5 | ll   |      88 |   74 |      97 |
|  6 | ff   |      75 |   72 |      93 |
|  7 | bb   |      87 |   86 |      76 |
+----+------+---------+------+---------+
4 rows in set (0.00 sec)

mysql> select sum(math) from exam;
+-------------------+
| sum(math)         |
+-------------------+
| 304.4000015258789 |
+-------------------+
1 row in set (0.00 sec)

mysql> select sum(math+chinese) from exam;
+-------------------+
| sum(math+chinese) |
+-------------------+
| 636.9000015258789 |
+-------------------+
1 row in set (0.00 sec)

mysql> select sum(math+chinese+english) from exam;
+---------------------------+
| sum(math+chinese+english) |
+---------------------------+
|        1001.4000015258789 |
+---------------------------+
1 row in set (0.00 sec)

mysql> 

其实像上面math+chinese,math+chinese+english都可以看作一列。


统计平均分用avg:

mysql> select avg(math+chinese+english) from exam;
+---------------------------+
| avg(math+chinese+english) |
+---------------------------+
|        250.35000038146973 |
+---------------------------+
1 row in set (0.00 sec)


看一下数学最高分,上面也演示过看最高分,不过用的方法是order by + limit的方式:

mysql> select max(math) from exam;
+-----------+
| max(math) |
+-----------+
|        86 |
+-----------+
1 row in set (0.00 sec)


看一下最低数学成绩:

mysql> select min(math) from exam;
+-----------+
| min(math) |
+-----------+
|        72 |
+-----------+
1 row in set (0.00 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/376787.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

密码学基础概念

把一段原始数据通过某种算法处理成另外一种数据&#xff08;原始数据为明文&#xff0c;处理后的数据为密文&#xff09;。明文->密文&#xff1a;称之为加密。密文->明文&#xff1a;称之为解密。 在加密过程中我们需要知道下面的这些概念&#xff1a; 1&#xff09;明文…

操作系统——10.进程通信

这篇文章我们来讲一下进程通信的相关内容 目录 1.概述 2.什么是进程通信 2.1进程通信——共享存储 2.2进程通信——管道通信 2.3进程通信——消息传递 3.小结 1.概述 首先&#xff0c;我们来看一下这节内容的大体框架 2.什么是进程通信 顾名思义&#xff0c;进程通信就是…

QML 鼠标事件

作者: 一去、二三里 个人微信号: iwaleon 微信公众号: 高效程序员 QML 中有一些元素本身是不具备交互能力的(例如:Rectangle、Text、Image 等),那么如何通过鼠标来控制它们的行为呢?这里就需要用到 MouseArea 元素了,它继承于 Item 且不可见,通常需要与可见元素结合使…

【vue2小知识】路由守卫的使用与解决RangeError: Maximum call stack size exceeded问题的报错。

&#x1f973;博 主&#xff1a;初映CY的前说(前端领域) &#x1f31e;个人信条&#xff1a;想要变成得到&#xff0c;中间还有做到&#xff01; &#x1f918;本文核心&#xff1a;当我们在路由跳转前与后我们可实现触发的操作 【前言】当我们在做类似于登录页面的时候&…

jmeter-如何在多线程一起执行时,控制请求的执行顺序【临界部分控制器】

前言&#xff1a;一个线程多个脚本的时候&#xff0c;发现只要100个用户同时执行&#xff0c;请求就会乱。期望2个线程执行结果&#xff1a;获取验证码-注册-登录这个流程获取验证码-注册-登录这个流程实际2个线程执行结果&#xff1a;a. 登录-获取验证码-注册b. 注册-获取验证…

运动无线蓝牙耳机哪款好、运动无线蓝牙耳机推荐

作为 运动爱好者&#xff0c;每天早晨醒来后的第一件事就去家门口的湿地公园跑上一圈。各种运动装备都齐了&#xff0c;不过在耳机选择上还真的犯难&#xff0c;打着“运动耳机”旗号的产品也是种类繁多&#xff0c;那么到底什么样的无线耳机更适合运动呢&#xff1f;于是我花时…

零基础入门网络安全,看这一篇就够了!

前景 很多零基础朋友开始将网络安全作为发展的大方向&#xff0c;的确&#xff0c;现如今网络安全已经成为了一个新的就业风口&#xff0c;不仅大学里开设相关学科&#xff0c;连市场上也开始大量招人。 那么网络安全到底前景如何&#xff1f;大致从市场规模、政策扶持、就业…

vxe-table 表格多选框回显

1.弹框表格结构 <a-modal v-if"visibleQuality" title"请选择需要提高的能力素质要求" :maskClosable"false" :visible"visibleQuality && switchStatus" ok"handleOkQuality" cancel"handleCancelQuality&…

【微信小程序-原生开发+TDesign】通用功能页封装——地点搜索(含腾讯地图开发key 的申请方法)

效果预览 核心技能点 调用腾讯地图官方的关键字地点搜索功能&#xff0c;详见官方文档 https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/methodGetsuggestion 完整代码实现 地点输入框 <t-input value"{{placeInfo.title}}" bindtap"searchPlace" dis…

华为OD机试模拟题 用 C++ 实现 - 端口合并(2023.Q1)

最近更新的博客 【华为OD机试模拟题】用 C++ 实现 - 最多获得的短信条数(2023.Q1)) 文章目录 最近更新的博客使用说明端口合并题目输入输出示例一输入输出说明示例二输入输出说明示例三输入输出说明

力扣299.猜数字游戏(java语言实现)

题目描述&#xff1a; 你在和朋友一起玩 猜数字&#xff08;Bulls and Cows&#xff09;游戏&#xff0c;该游戏规则如下&#xff1a; 写出一个秘密数字&#xff0c;并请朋友猜这个数字是多少。朋友每猜测一次&#xff0c;你就会给他一个包含下述信息的提示&#xff1a; 猜测…

JVM系统优化实践(5):什么时候GC以及有哪些GC

您好&#xff0c;我是湘王&#xff0c;这是我的CSDN博客&#xff0c;欢迎您来&#xff0c;欢迎您再来&#xff5e;既然程序运行会产生大量的废弃物&#xff0c;也就是「垃圾」&#xff0c;那总不能一直堆着不管吧。现在就来粗浅地谈谈Java里面什么时候会触发GC以及有哪些GC。通…

NBA Top Shot 跌落神坛

近日&#xff0c;美国职业篮球联盟&#xff08;NBA&#xff09;授权的NFT 项目“NBA Top Shot Moments”被纽约法院初步裁定为“可能符合证券的定义”&#xff0c;虽然这不是对2021年用户指控该项目违法的最终判决&#xff0c;但这个裁定引发了市场担忧&#xff0c;部分NFT的地…

Java的运算操作

个人主页&#xff1a;平行线也会相交 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 平行线也会相交 原创 收录于专栏【JavaSE_primary】 文章目录算术运算符增量运算符注意自增自减运算符关系运算符逻辑运算符逻辑与&&逻辑或||逻辑非&#xff01;…

直觉外科:手术机器人领域的领导者,未来还有巨大的增长空间

来源&#xff1a;猛兽财经 作者&#xff1a;猛兽财经 虽然直觉外科(ISRG)多年来一直在强劲的增长&#xff0c;但猛兽财经认为它未来仍有足够的空间进一步扩大收入和利润。虽然这个行业的竞争正在加剧&#xff0c;但猛兽财经认为从长远来看&#xff0c;这个市场可能会整合&…

如何修改JAR包内的代码

有时候由于找不到源码&#xff0c;只有一个jar包&#xff0c;但又想去修改jar包中的代码&#xff0c;就可以进行将jar包反编译后&#xff0c;修改&#xff0c;再重新编译的方式来实现。 一、下载反编译软件JD-GUI https://github.com/java-decompiler/jd-gui/releases 二、用…

Android 项目必备(四十二)-->Android 多窗口模式

简介 自由窗口模式: 该模式类似于常见的桌面操作系统&#xff0c; 应用界面的窗口可以自由的拖动和修改大小。 分屏模式 该模式可以在手机上使用&#xff0c; 该模式将屏幕一分为二&#xff0c; 同时显示两个应用界面。 画中画模式: 该模式主要用于TV&#xff0c; 在该模式下…

项目--基于RTSP协议的简易服务器开发(1)

RTSP协议简介&#xff1a;RTSP&#xff08;Real Time Streaming Prcotol&#xff09;是位于TCP\IP体系应用层的数据传输协议&#xff0c;通常的RTSP协议包含 RTSP、RTP、RTCP协议。一般而言&#xff1a;RTSP负责服务器与客户端之间的请求与响应RTP负责服务器、客户端之间传输媒…

【双U网络:传感器间自监督:全色锐化】

W-NetPan: Double-U network for inter-sensor self-supervised pan-sharpening &#xff08;W-NetPan&#xff1a;用于传感器间自监督全色锐化的双U网络&#xff09; 由于遥感数据的提供日益增多&#xff0c;可以通过全色锐化方法来处理空间-光谱限制。然而&#xff0c;融合…

【JavaGuide面试总结】操作系统篇·上

【JavaGuide面试总结】操作系统篇上1.什么是系统调用呢&#xff1f; 能不能详细介绍一下2.进程和线程的区别3.进程有哪几种状态?4.进程间的通信常见的的有哪几种方式呢?5.线程间的同步的方式有哪些呢?6.操作系统中进程的调度算法有哪些7.产生死锁的四个必要条件是什么?8.解…