MySQL —— 基本查询

2023-11-09


前言: 本文主要讲解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)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MySQL —— 基本查询 的相关文章

  • 拥有更多列或更多行会更高效吗?

    我目前正在重新设计一个可能包含大量数据的数据库 我可以选择在数据库中包含许多不同的列或使用大量行 如果我在下面做一些大纲 可能会更容易 item id user id title description content category t
  • 如何从 MySQL 数据查询创建 XML 文件?

    我想知道一种仅使用 MySQL 查询创建 XML 文件的方法 根本不使用任何脚本语言 有关于这个主题的书籍 教程吗 UPDATE 我想澄清一下 我想使用 sql 查询将 XML 数据转发到 php 脚本 Here s 关于从 MySQL S
  • eclipse中导入项目文件夹图标

    我在 Eclipse 工作区中新导入的 Maven 项目有J and M项目文件夹顶部的图标 项目和包资源管理器 而其他导入的 Maven 项目只有一个J icon 有人可以解释其中的区别吗 该项目有J装饰器被称为 Java 项目和具有M装
  • 使用 RecyclerView 适配器在运行时更改布局屏幕

    我有两个布局文件 如下所示 如果列表中存在数据 则我显示此布局 当列表为空时 我会显示此布局 现在我想在运行时更改布局 当用户从列表中删除最后一项时 我想将布局更改为第二张图片中显示的 空购物车布局 In getItemCount Recy
  • Condition 接口中的 signalAll 与对象中的 notificationAll

    1 昨天我才问过这个问题条件与等待通知机制 https stackoverflow com questions 10395571 condition vs wait notify mechanism 2 我想编辑相同的内容并在我的问题中添加
  • MySQL如何获取unix时间戳的时间差

    我有一个保存值1506947452的变量 需要使用公式从该日期提取分钟 started data now date 但started date采用unix时间戳格式10位int数字 我以ajax形式收到并需要放入mysql查询i试试这个 S
  • 如何让spring为JdbcMetadataStore创建相应的schema?

    我想使用此处描述的 jdbc 元数据存储 https docs spring io spring integration docs 5 2 0 BUILD SNAPSHOT reference html jdbc html jdbc met
  • 计算日期之间的天数差异

    在我的代码中 日期之间的差异是错误的 因为它应该是 38 天而不是 8 天 我该如何修复 package random04diferencadata import java text ParseException import java t
  • Java - 返回值是否会中断循环?

    我正在编写一些基本上遵循以下格式的代码 public static boolean isIncluded E element Node
  • Cloudfoundry:如何组合两个运行时

    cloundfoundry 有没有办法结合两个运行时环境 我正在将 NodeJS 应用程序部署到 IBM Bluemix 现在 我还希望能够执行独立的 jar 文件 但应用程序失败 APP 0 bin sh 1 java not found
  • Spring Security OAuth2简单配置

    我有一个简单的项目 需要以下简单的配置 我有一个 密码 grant type 这意味着我可以提交用户名 密码 用户在登录表单中输入 并在成功时获得 access token 有了该 access token 我就可以请求 API 并获取用户
  • Android Studio 将音乐文件读取为文本文件,如何恢复它?

    gameAlert mp3是我的声音文件 运行应用程序时 它询问我该文件不与任何文件类型关联 请定义关联 我选择TextFile错误地 现在我的音乐文件被读取为文本文件 我如何将其转换回music file protected void o
  • Espresso 和 Proguard 的 Java.lang.NoClassDefFoundError

    我对 Espresso 不太有经验 但我终于成功地运行了它 我有一个应用程序需要通过 Proguard 缩小才能处于 56K 方法之下 该应用程序以 3 秒的动画开始 因此我需要等到该动画结束才能继续 这就是我尝试用该方法做的事情waitF
  • 为什么java中的for-each循环中需要声明变量

    for 每个循环的通常形式是这样的 for Foo bar bars bar doThings 但如果我想保留 bar 直到循环结束 我可以not使用 foreach 循环 Foo bar null Syntax error on toke
  • JVM:是否可以操作帧堆栈?

    假设我需要执行N同一线程中的任务 这些任务有时可能需要来自外部存储的一些值 我事先不知道哪个任务可能需要这样的值以及何时 获取速度要快得多M价值观是一次性的而不是相同的M值在M查询外部存储 注意我不能指望任务本身进行合作 它们只不过是 ja
  • 哪个集合更适合存储多维数组中的数据?

    我有一个multi dimensional array of string 我愿意将其转换为某种集合类型 以便我可以根据自己的意愿添加 删除和插入元素 在数组中 我无法删除特定位置的元素 我需要这样的集合 我可以在其中删除特定位置的数据 也
  • JSON 到 hashmap (杰克逊)

    我想将 JSON 转换为 HashMapJackson http jackson codehaus org 这是我的 JSON String json Opleidingen name Bijz trajecten zorg en welz
  • Java:多线程内的 XA 事务传播

    我如何使用事务管理器 例如Bitronix http docs codehaus org display BTM Home JBoss TS http www jboss org jbosstm or Atomikos http www a
  • 嵌入式 Jetty - 以编程方式添加基于表单的身份验证

    有没有一种方法可以按如下方式以编程方式添加基于表单的身份验证 我用的是我自己的LdapLoginModule 最初我使用基本身份验证并且工作正常 但现在我想在登录页面上进行更多控制 例如显示徽标等 有没有好的样品 我正在使用嵌入式 jett
  • 使用 PHP 将 latin1_swedish_ci 转换为 utf8

    我有一个数据库 里面充满了类似的值 Dhaka 应该是 Dhaka 因为我在创建数据库时没有指定排序规则 现在我想修复它 我无法从最初获取数据的地方再次获取数据 所以我在想是否可以在 php 脚本中获取数据并将其转换为正确的字符 我已将数据

随机推荐

  • 最短路径算法 迪杰斯特拉、佛洛依德和贝尔曼

    最短路径算法 迪杰斯特拉算法 佛洛依德算法 迪杰斯特拉算法 迪杰斯特拉算法用来解决在有向有权图中某一个点到任意一点的最短路径问题 注意 只能用来解决权为非零的情况 不能够解决权为负数的情况 思想 我是一个搬运工 思想就不讲了 主要是代码 d
  • Vue基础入门---Vue-router

    简介 由于Vue在开发时对路由支持的不足 后来官方补充了vue router插件 它在Vue的生态环境中非常重要 在实际开发中只要编写一个页面就会操作vue router 要学习vue router就要先知道这里的路由是什么 这里的路由并不
  • vps用途

    VPS用途 1 虚拟主机空间 VPS主机非常适合为中小企业 小型门户网站 个人工作室 SOHO一族提供网站空间 较大独享资源 安全可靠的隔离保证了用户对于资源的使用和数据的安全 2 电子商务平台 VPS主机与独立服务器的运行完全相同 中小型
  • STL教程5-STL基本概念及String和vector使用

    活动地址 毕业季 进击的技术er 夏日炎炎 热浪中我们迎来毕业季 这是告别 也是迈向新起点的开始 CSDN诚邀各毕业生 在校生 职场人讲述自己的毕业季故事 分享自己的经验 技术er的进击之路 等你来书写 你可以选择适合自己的对应身份从以下相
  • Calendar根据当前(指定)日期取出指定时间

    以下为亲测 持续更新 一 Calendar根据当前 指定 日期取出本周一本周日和下周一下周日时间 根据Calendar java中定义的DAY OF WEEK来看 Field number for code get code and cod
  • 【LeetCode104】二叉树的最大深度(递归+迭代)

    题目描述 首刷自解 int maxDepth TreeNode root if root nullptr return 0 return max maxDepth root gt left maxDepth root gt right 1
  • k-means算法Python实现--机器学习ML

    k means algorithm 一些概念 partial clustering 每一簇的数据不重叠 至少一簇一个数据 hieraichical clustering 通过构建层次结构来确定聚类分配 density based clust
  • 设计模式-访问者模式

    1 访问者模式被称为最复杂的设计模式 访问者模式访问者模式 Visitor pattern 是一种将数据结构与数据操作分离的设计模式 是指封装一些作用于某种数据结构中的各元素的操作 它可以在不改变数据结构的前提下定义作用于这些元素的新的操作
  • png透明通道分离

    关于photoshop中png打开问题 前面也说到过http blog csdn net shenmifangke article details 52638716 ps在打开png格式图片的时候 实际上是把透明通道应用到了所有通道上 这样
  • IndentationError: unindent does not match any outer indentation level

    IndentationError unindent does not match any outer indentation level 遇到这个错误 是因为新的Python语法中是不支持的代码对齐中 混用TAB和空格的 解决方法 使用工具
  • 【C语言】之实现闰年判断

    文件名 leapYear c 功能 任意输入一个年份 判断其是否为闰年 编辑人 王廷云 include
  • VB+SQL银行设备管理系统设计与实现

    摘要 随着银行卡的普及 很多地方安装了大量的存款机 取款机和POS机等银行自助设备 银行设备管理系统可以有效的记录银行设备的安装和使用情况 规范对自助设备的管理 从而为用户提供更加稳定和优质的服务 本文介绍了银行设备管理系统的设计和开发过程
  • ooad设计模型

    设计模式 Design pattern 是一套被反复使用 多数人知晓的 经过分类编目的 代码设计经验的总结 使用设计模式是为了可重用代码 让代码更容易被他人理解 保证代码可靠性 毫无疑问 设计模式于己于他人于系统都是多赢的 设计模式使代码编
  • 前端之vue3使用动画库animate.css(含动画、过渡)

    动画与过渡 一 动画效果 1 默认动画 实例 动画语法 2 给transition指定name 二 过渡效果 三 多个元素过渡 四 vue3使用动画库 动画库animate css 五 总结 一 动画效果 1 默认动画 实例
  • github怎么自动更新被人更新过的项目_新出炉的免费开源平台,GitHub官方出品

    软件名称 GitHub Desktop 2 2 4 安装环境 Windows 下载链接 下载链接 https www sssam com 10822 html 软件简介 GitHub是一个面向开源及私有软件项目的托管平台 因为只支持git
  • 前端实现websocket(vue3)

    在config index js文件中配置一下websocket websocket的域名和端口号的配置 const BASE URL localhost const WS PORT 8080 const WS ADDRESS ws BAS
  • linux 查看连接数,并发数

    软连接 ln s home ictfmcg data photo var jtnd data photo tomcat 6的Connector配置如下
  • MAX30102 高灵敏度脉搏氧器和心率传感器说明书

    MAX30102 一 简介 MAX30102是一个集成的脉搏血氧测量和心率监测模块 它包括内部led 光电探测器 光学元件和具有环境光排斥作用的低噪声电子器件 MAX30102提供了一个完整的系统解决方案 以简化移动和可穿戴设备的设计过程
  • 【Hadoop 01】简介

    目录 1 Hadoop 简介 2 下载并配置Hadoop 2 1 修改 etc profile 2 2 修改hadoop env sh 2 3 修改core site xml 2 4 修改hdfs site xml 2 5 修改mapred
  • MySQL —— 基本查询

    文章目录 1 向表中插入数据 2 查询操作 2 1 全列查询 2 2 指定列查询 2 3 查询字段带表达式 2 4 为查询结果指定别名 2 5 去重操作 3 where 条件 3 1 比较运算符和逻辑预算符的运用 3 2 like的细节 3