MySQL之表内容的增删改查(含oracel 9i经典测试雇佣表下载)

目录

一:Create

二:Retrieve 

1.select列

 2.where条件

3.结果排序 

4. 筛选分页结果 

三:Update

四:Delete 

1.删除数据

2. 截断表

五:插入查询结果 

六:聚合函数 

七:group by子句的使用 


表内容的CRUD操作 : Create(创建), Retrieve(读取),Update(更新),Delete(删除)

一:Create

(1)语法: insert into 表名 (列名) values (对应的列属性值)

(2)单行及多行插入

mysql> system clear;
mysql> create table student(
    -> id int unsigned primary key auto_increment,
    -> sn int unsigned not null unique key,
    -> name varchar(20) not null,
    -> qq varchar(20) unique key
    -> );
Query OK, 0 rows affected (0.03 sec)

#单行指定列插入
mysql> insert into student (id,sn,name,qq) values (10,201,'刘备','230211');
Query OK, 1 row affected (0.01 sec)

#全列插入
mysql> insert into student values (20,202,'关羽','230212');
Query OK, 1 row affected (0.00 sec)

#单行指定列插入
mysql> insert into student (sn,name,qq) values (203,'张飞','230213');
Query OK, 1 row affected (0.01 sec)

#多行指定列插入
mysql> insert into student (sn,name,qq) values (204,'赵云','230214'),(205,'黄忠','230215');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select* from student;
+----+-----+--------+--------+
| id | sn  | name   | qq     |
+----+-----+--------+--------+
| 10 | 201 | 刘备   | 230211 |
| 20 | 202 | 关羽   | 230212 |
| 21 | 203 | 张飞   | 230213 |
| 22 | 204 | 赵云   | 230214 |
| 23 | 205 | 黄忠   | 230215 |
+----+-----+--------+--------+

(3) 插入否则更新

由于主键或者唯一键对应的值已经存在而导致插入失败

#主键冲突
mysql> insert into student (id,sn,name,qq) values (10,206,'马超','230216');
ERROR 1062 (23000): Duplicate entry '10' for key 'PRIMARY'
#唯一键冲突
mysql> insert into student (id,sn,name,qq) values (24,205,'马超','230216');
ERROR 1062 (23000): Duplicate entry '205' for key 'sn'

插入替换1:不存在则直接插入,存在的话修改下里面某一列的数据

mysql> select* from student;
+----+-----+--------+--------+
| id | sn  | name   | qq     |
+----+-----+--------+--------+
| 10 | 201 | 刘备   | 230211 |
| 20 | 202 | 关羽   | 230212 |
| 21 | 203 | 张飞   | 230213 |
| 22 | 204 | 赵云   | 230214 |
| 23 | 205 | 黄忠   | 230215 |
+----+-----+--------+--------+
5 rows in set (0.00 sec)

mysql> insert into student values (23,205,'huangzhong','230216') on duplicate key update name='huanghzong',qq='230216';
Query OK, 2 rows affected (0.00 sec)

mysql> select* from student;
+----+-----+------------+--------+
| id | sn  | name       | qq     |
+----+-----+------------+--------+
| 10 | 201 | 刘备       | 230211 |
| 20 | 202 | 关羽       | 230212 |
| 21 | 203 | 张飞       | 230213 |
| 22 | 204 | 赵云       | 230214 |
| 23 | 205 | huanghzong | 230216 |
+----+-----+------------+--------+
5 rows in set (0.00 sec)

mysql> insert into student values (24,207,'马超','230217') on duplicate key update name='马超';
Query OK, 1 row affected (0.01 sec)

mysql> select* from student;
+----+-----+------------+--------+
| id | sn  | name       | qq     |
+----+-----+------------+--------+
| 10 | 201 | 刘备       | 230211 |
| 20 | 202 | 关羽       | 230212 |
| 21 | 203 | 张飞       | 230213 |
| 22 | 204 | 赵云       | 230214 |
| 23 | 205 | huanghzong | 230216 |
| 24 | 207 | 马超       | 230217 |
+----+-----+------------+--------+
6 rows in set (0.00 sec)

mysql> insert into student values (10,201,'刘备','230211') on duplicate key update name='刘备',qq='230211';
Query OK, 0 rows affected (0.00 sec)



-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,并且数据已经被更新
-- 通过 MySQL 函数获取受到影响的数据行数
SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
|      2 |
+-------------+

要保证更新后的数据不能与其它主键,唯一键冲突 

插入替换2:

replace into 表名 (列名) values (对应列名属性值)

-- 主键 或者 唯一键 没有冲突,则直接插入;
-- 主键 或者 唯一键 如果冲突,则删除后再插入

mysql> replace into student values (24,207,'machao','230217');
Query OK, 2 rows affected (0.00 sec)

mysql> select* from student;
+----+-----+------------+--------+
| id | sn  | name       | qq     |
+----+-----+------------+--------+
| 10 | 201 | 刘备       | 230211 |
| 20 | 202 | 关羽       | 230212 |
| 21 | 203 | 张飞       | 230213 |
| 22 | 204 | 赵云       | 230214 |
| 23 | 205 | huanghzong | 230216 |
| 24 | 207 | machao     | 230217 |
+----+-----+------------+--------+


-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,删除后重新插入

二:Retrieve 

1.select列

(1) 全列查询 select* from 表名

通常情况下不建议使用 * 进行全列查询
-- 1. 查询的列越多,意味着需要传输的数据量越大;
-- 2. 可能会影响到索引的使用

(2)指定列查询

mysql> select id,name from student;
+----+------------+
| id | name       |
+----+------------+
| 10 | 刘备       |
| 20 | 关羽       |
| 21 | 张飞       |
| 22 | 赵云       |
| 23 | huanghzong |
| 24 | machao     |
+----+------------+

(3) 查询字段为表达式

mysql> select* from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)

mysql> select id,name,math+10 from exam_result;
+----+-----------+---------+
| id | name      | math+10 |
+----+-----------+---------+
|  1 | 唐三藏    |     108 |
|  2 | 孙悟空    |      88 |
|  3 | 猪悟能    |     108 |
|  4 | 曹孟德    |      94 |
|  5 | 刘玄德    |      95 |
|  6 | 孙权      |      83 |
|  7 | 宋公明    |      75 |
+----+-----------+---------+
7 rows in set (0.00 sec)

//查询结果取别名,也可以不加as
mysql> select id,name,chinese+math+english as total from exam_result;
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  1 | 唐三藏    |   221 |
|  2 | 孙悟空    |   242 |
|  3 | 猪悟能    |   276 |
|  4 | 曹孟德    |   233 |
|  5 | 刘玄德    |   185 |
|  6 | 孙权      |   221 |
|  7 | 宋公明    |   170 |
+----+-----------+-------+

(4) 结果去重

mysql> select math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   98 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+
7 rows in set (0.00 sec)

mysql> select distinct math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+

 2.where条件

比较运算符:

逻辑运算符:and     or     not

举例:

mysql> select* from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+

(1)英语不及格的同学及英语成绩 ( < 60 )

mysql> select id,name,english from exam_result where english<60;
+----+-----------+---------+
| id | name      | english |
+----+-----------+---------+
|  1 | 唐三藏    |      56 |
|  5 | 刘玄德    |      45 |
|  7 | 宋公明    |      30 |
+----+-----------+---------+

(2)语文成绩在 [80, 90] 分的同学及语文成绩

mysql> select name,chinese from exam_result where chinese>=80 and chinese<=90;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 孙悟空    |      87 |
| 猪悟能    |      88 |
| 曹孟德    |      82 |
+-----------+---------+
3 rows in set (0.00 sec)

mysql> select name,chinese from exam_result where chinese between 80 and 90;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 孙悟空    |      87 |
| 猪悟能    |      88 |
| 曹孟德    |      82 |
+-----------+---------+

(3)数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩

mysql> select name,math from exam_result where math=58 or math=59 or math=98 or math=99;
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 猪悟能    |   98 |
+-----------+------+
2 rows in set (0.00 sec)

mysql> select name,math from exam_result where math in(58,59,98,99);
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 猪悟能    |   98 |
+-----------+------+

(4)姓孙的同学 及 孙某同学

#-- % 匹配任意多个(包括 0 个)任意字符
mysql> select name from exam_result where name like '孙%';
+-----------+
| name      |
+-----------+
| 孙悟空    |
| 孙权      |
+-----------+
2 rows in set (0.00 sec)

#-- _ 匹配严格的一个任意字符
mysql> select name from exam_result where name like '孙_';
+--------+
| name   |
+--------+
| 孙权   |
+--------+

(5) 语文成绩好于英语成绩的同学

mysql> select name,chinese,english from exam_result where chinese>english;
+-----------+---------+---------+
| name      | chinese | english |
+-----------+---------+---------+
| 唐三藏    |      67 |      56 |
| 孙悟空    |      87 |      77 |
| 曹孟德    |      82 |      67 |
| 刘玄德    |      55 |      45 |
| 宋公明    |      75 |      30 |
+-----------+---------+---------+

 (6)总分在 200 分以下的同学

mysql> select name,chinese+math+english total from exam_result where chinese+english+math<200;
+-----------+-------+
| name      | total |
+-----------+-------+
| 刘玄德    |   185 |
| 宋公明    |   170 |
+-----------+-------+

#这里不可以使用前面起的别名在where子句中做判断
#这里有执行顺序的:from语句 ->  where语句 ->select语句
mysql> select name,chinese+math+english total from exam_result where total<200;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'

(7)语文成绩 > 80 并且不姓孙的同学

mysql> select name,chinese from exam_result where chinese>80 and (name not like '孙%');
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 猪悟能    |      88 |
| 曹孟德    |      82 |
+-----------+---------+

(8) 孙某同学,否则要求总成绩 > 200 并且 语文成绩 < 数学成绩 并且 英语成绩 > 80

mysql> select name,chinese+math+english total from exam_result where(chinese+english+math>200 and math>chinese and english>80) or name like '孙_';
+-----------+-------+
| name      | total |
+-----------+-------+
| 猪悟能    |   276 |
| 孙权      |   221 |
+-----------+-------+

(9)NULL查询 

SELECT name, qq FROM students WHERE qq IS NOT NULL;
+-----------+-------+
| name | qq |
+-----------+-------+
| 孙悟空 | 11111 |
+-----------+-------+
1 row in set (0.00 sec)

-- NULL 和 NULL 的比较,= 和 <=> 的区别
SELECT NULL = NULL, NULL = 1, NULL = 0;
+-------------+----------+----------+
| NULL = NULL | NULL = 1 | NULL = 0 |
+-------------+----------+----------+
| NULL | NULL | NULL |
+-------------+----------+----------+
1 row in set (0.00 sec)

SELECT NULL <=> NULL, NULL <=> 1, NULL <=> 0;
+---------------+------------+------------+
| NULL <=> NULL | NULL <=> 1 | NULL <=> 0 |
+---------------+------------+------------+
| 1 | 0 | 0 |
+---------------+------------+------------+

3.结果排序 

语法:

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

select ... from... order by 列名 (asc/desc),...

(1)同学及数学成绩,按数学成绩升序显示

mysql> select name,math from exam_result order by math asc;
+-----------+------+
| name      | math |
+-----------+------+
| 宋公明    |   65 |
| 孙权      |   73 |
| 孙悟空    |   78 |
| 曹孟德    |   84 |
| 刘玄德    |   85 |
| 唐三藏    |   98 |
| 猪悟能    |   98 |
+-----------+------+

(2) 同学及 qq 号,按 qq 号排序显示

-- NULL 视为比任何值都小,升序出现在最上面

select name,qq from students order by qq;
+-----------+-------+
| name | qq |
+-----------+-------+
| 唐大师 | NULL |
| 孙仲谋 | NULL |
| 曹阿瞒 | NULL |
| 孙悟空 | 11111 |

(3) 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示 

mysql> select name,math,chinese,english from exam_result order by math desc, english asc,chinese asc;
+-----------+------+---------+---------+
| name      | math | chinese | english |
+-----------+------+---------+---------+
| 唐三藏    |   98 |      67 |      56 |
| 猪悟能    |   98 |      88 |      90 |
| 刘玄德    |   85 |      55 |      45 |
| 曹孟德    |   84 |      82 |      67 |
| 孙悟空    |   78 |      87 |      77 |
| 孙权      |   73 |      70 |      78 |
| 宋公明    |   65 |      75 |      30 |
+-----------+------+---------+---------+

(4)查询同学及总分,由高到低

-- ORDER BY 子句中可以使用列别名
#语句的执行顺序为: form语句 -> where语句 -> select语句 -> order by语句
mysql> select name,chinese+english+math total from exam_result order by total desc;
+-----------+-------+
| name      | total |
+-----------+-------+
| 猪悟能    |   276 |
| 孙悟空    |   242 |
| 曹孟德    |   233 |
| 唐三藏    |   221 |
| 孙权      |   221 |
| 刘玄德    |   185 |
| 宋公明    |   170 |
+-----------+-------+

(5) 查询姓孙的同学或者姓曹的同学数学成绩,结果按数学成绩由高到低显示 

mysql> select name,math from exam_result where name like '孙%' or name like '曹%' order by math desc;
+-----------+------+
| name      | math |
+-----------+------+
| 曹孟德    |   84 |
| 孙悟空    |   78 |
| 孙权      |   73 |
+-----------+------+

4. 筛选分页结果 

语法:

-- 起始下标为 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,chinese,math,english from exam_result limit 3 offset 0;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)

mysql> select id,name,chinese,math,english from exam_result limit 3 offset 3;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)

mysql> select id,name,chinese,math,english from exam_result limit 3 offset 6;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+

 建议:对未知表进行查询时,最好加一条 LIMIT 1,避免因为表中数据过大,查询全表数据导致数据库卡死
按 id 进行分页,每页 3 条记录,分别显示 第 1、2、3 页

三:Update

语法: update 表名 set 列名=列属性值 where 匹配条件 (order by ..) (limit ...)

对查询到的结果进行列值更新

举例:

(1)将孙悟空同学的数学成绩变更为 80 分

mysql> select* from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)

mysql> update exam_result set math=80 where name='孙悟空';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0


mysql> select name,math from exam_result where name='孙悟空';
+-----------+------+
| name      | math |
+-----------+------+
| 孙悟空    |   80 |
+-----------+------+

(2)将曹孟德同学的数学成绩变更为 60 分,语文成绩变更为 70 分

mysql> select name,math,chinese from exam_result where name='曹孟德';
+-----------+------+---------+
| name      | math | chinese |
+-----------+------+---------+
| 曹孟德    |   84 |      82 |
+-----------+------+---------+
1 row in set (0.00 sec)

mysql> update exam_result set math=60,chinese=70 where name='曹孟德';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select name,math,chinese from exam_result where name='曹孟德';
+-----------+------+---------+
| name      | math | chinese |
+-----------+------+---------+
| 曹孟德    |   60 |      70 |
+-----------+------+---------+

(3)将总成绩倒数前三的 3 位同学的数学成绩加上 30 分 

mysql> select name,chinese+math+english total from exam_result order by total asc limit 3;
+-----------+-------+
| name      | total |
+-----------+-------+
| 宋公明    |   170 |
| 刘玄德    |   185 |
| 曹孟德    |   197 |
+-----------+-------+
3 rows in set (0.00 sec)

mysql> update exam_result set math=math+30 order by math+chinese+english asc limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select name,chinese+math+english total from exam_result order by total asc limit 3;
+-----------+-------+
| name      | total |
+-----------+-------+
| 宋公明    |   200 |
| 刘玄德    |   215 |
| 唐三藏    |   221 |
+-----------+-------+

(4)  将所有同学的语文成绩更新为原来的 2 倍

-- 没有 WHERE 子句,则更新全表

mysql> select name,chinese from exam_result;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 唐三藏    |      67 |
| 孙悟空    |      87 |
| 猪悟能    |      88 |
| 曹孟德    |      70 |
| 刘玄德    |      55 |
| 孙权      |      70 |
| 宋公明    |      75 |
+-----------+---------+
7 rows in set (0.00 sec)

mysql> update exam_result set chinese=chinese*2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7  Changed: 7  Warnings: 0

mysql> select name,chinese from exam_result;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 唐三藏    |     134 |
| 孙悟空    |     174 |
| 猪悟能    |     176 |
| 曹孟德    |     140 |
| 刘玄德    |     110 |
| 孙权      |     140 |
| 宋公明    |     150 |
+-----------+---------+

四:Delete 

1.删除数据

语法: delete from 表名 where 匹配条件 (order by...) (limit ...)

举例: (1) 删除孙悟空同学的考试成绩

mysql> select* from exam_result where name='孙悟空';
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  2 | 孙悟空    |     174 |   80 |      77 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)

mysql> delete from exam_result where name='孙悟空';
Query OK, 1 row affected (0.01 sec)

mysql> select* from exam_result where name='孙悟空';
Empty set (0.00 sec)

 (2)删除整张表数据

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

mysql> INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
Query OK, 3 rows affected (0.00 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> show create table for_delete\G;
*************************** 1. row ***************************
       Table: for_delete
Create Table: CREATE TABLE `for_delete` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

#删除整表数据
mysql> delete from for_delete;
Query OK, 3 rows affected (0.01 sec)

#再插入一条数据,自增 id 在原值上增长
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> show create table for_delete \G;
*************************** 1. row ***************************
       Table: for_delete
Create Table: CREATE TABLE `for_delete` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

这里是不会将自增序列对应的值置为0的

2. 截断表

语法:truncate (table)  表名

1. 只能对整表操作,不能像 DELETE 一样针对部分数据操作;
2. 实际上 MySQL 不对数据操作,所以比 DELETE 更快,但是TRUNCATE在删除数据的时候,并不经过真正的事物,所以无法回滚
3. 会重置 AUTO_INCREMENT 项

4.作用也是会将表的数据内容清空

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.00 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)

-- 截断整表数据,注意影响行数是 0,所以实际上没有对数据真正操作
mysql> truncate table for_truncate;
Query OK, 0 rows affected (0.01 sec)

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

-- 再插入一条数据,自增 id 在重新增长
mysql> insert into for_truncate (name) values ('Q');
Query OK, 1 row affected (0.00 sec)

mysql> select* from for_truncate;
+----+------+
| id | name |
+----+------+
|  1 | Q    |
+----+------+

五:插入查询结果 

 语法: insert into table_name [(column [, column ...])]  select ...

举例:删除表中的的重复复记录,重复的数据只能有一份

mysql> CREATE TABLE duplicate_table (id int, name varchar(20));
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO duplicate_table VALUES
    -> (100, 'aaa'),
    -> (100, 'aaa'),
    -> (200, 'bbb'),
    -> (200, 'bbb'),
    -> (200, 'bbb'),
    -> (300, 'ccc');
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

# 创建一张空表 no_duplicate_table,结构和 duplicate_table 一样
mysql> create table no_duplicate_table like duplicate_table;
Query OK, 0 rows affected (0.02 sec)

# 将duplicate_table 的去重数据插入到 no_duplicate_table
mysql> INSERT INTO no_duplicate_table SELECT DISTINCT * FROM duplicate_table;
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

# 通过重命名表,实现原子的去重操作
mysql> RENAME TABLE duplicate_table TO old_duplicate_table,
    -> no_duplicate_table TO duplicate_table;
Query OK, 0 rows affected (0.02 sec)

mysql> SELECT * FROM duplicate_table;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+

六:聚合函数 

举例:

(1)统计班级共有多少同学

mysql> select* from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |     134 |   98 |      56 |
|  3 | 猪悟能    |     176 |   98 |      90 |
|  4 | 曹孟德    |     140 |   90 |      67 |
|  5 | 刘玄德    |     110 |  115 |      45 |
|  6 | 孙权      |     140 |   73 |      78 |
|  7 | 宋公明    |     150 |   95 |      30 |
+----+-----------+---------+------+---------+
6 rows in set (0.00 sec)

mysql> select count(*) from exam_result;
+----------+
| count(*) |
+----------+
|        6 |
+----------+

(2)统计班级收集的id 号有多少 

mysql> select count(id) from exam_result;
+-----------+
| count(id) |
+-----------+
|         6 |
+-----------+

(3)统计本次考试的数学成绩分数个数 

mysql> select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
|           6 |
+-------------+
1 row in set (0.00 sec)

//统计去重成绩数量
mysql> select count( distinct math) from exam_result;
+-----------------------+
| count( distinct math) |
+-----------------------+
|                     5 |
+-----------------------+

(4)统计数学成绩总分

mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
|       569 |
+-----------+

(5)统计平均分

mysql> select avg(chinese+math+english) total from exam_result;
+-------+
| total |
+-------+
| 297.5 |
+-------+

(6)得到英语最高分 

mysql> select max(english) from exam_result;
+--------------+
| max(english) |
+--------------+
|           90 |
+--------------+

 (7)返回 > 70 分以上的数学最低分

mysql> select math from exam_result where math>70;
+------+
| math |
+------+
|   98 |
|   98 |
|   90 |
|  115 |
|   73 |
|   95 |
+------+
6 rows in set (0.00 sec)

mysql> select  min(math) from exam_result where math>70;
+-----------+
| min(math) |
+-----------+
|        73 |
+-----------+

七:group by子句的使用 

在select中使用group by 子句可以对指定列进行分组查询
语法:select column1, column2, .. from table group by column;

分组的目的是为了进行分组之后,方便进行聚合统计。分组实质就是把一组按照条件拆成了多个组,进行各自组内的统计,也可以理解为是把一张表按照条件在逻辑上拆成了多个子表,然后分别对各自的子表进行聚合统计

这里指定列名,是在分组时用该列的不同行数据来进行分组的,同组可以聚合压缩

举例:

准备工作,创建一个雇员信息表(来自oracle 9i的经典测试表)
EMP员工表
DEPT部门表
SALGRADE工资等级表

经典测试雇佣表

选择一个文件目录,左键在终端中打开

将复制后的指令在终端框中执行,这样在该文件目录下就会生成对应的文件

接下来进入Linux用户的家目录,进行rz上传这个文件

最后在mysql中soruce 文件目录导入表

[root@hcss-ecs-889f MySQL]# cd ~
[root@hcss-ecs-889f ~]# ll
total 39092
-rw-r--r--  1 root root        0 May 14 22:49 test1.c
[root@hcss-ecs-889f ~]# rz

[root@hcss-ecs-889f ~]# ll
total 39096
-rw-r--r--  1 root root     3878 Sep 17 11:03 scott_data.sql
-rw-r--r--  1 root root        0 May 14 22:49 test1.c
[root@hcss-ecs-889f ~]# pwd
/root

[root@hcss-ecs-889f ~]# mysql -uroot -p;

mysql> use d2;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> source /root/scott_data.sql;
Query OK, 0 rows affected, 1 warning (0.00 sec)


Query OK, 1 row affected (0.00 sec)

mysql> show tables;
+-----------------+
| Tables_in_scott |
+-----------------+
| dept            |
| emp             |
| salgrade        |
+-----------------+

(1)显示每个部门的平均工资和最高工资

select deptno,avg(sal),max(sal) from EMP group by deptno;

(2)显示每个部门的每种岗位的平均工资和最低工资

select avg(sal),min(sal),job, deptno from EMP group by deptno, job;

(3)显示平均工资低于2000的部门和它的平均工资

#统计各个部门的平均工资
select avg(sal) from EMP group by deptno;

#having和group by配合使用,对group by结果进行过滤,条件筛选
select avg(sal) as myavg from EMP group by deptno having myavg<2000;

#having经常和group by搭配使用,作用是对分组聚合之后的结果进行条件筛选,
#where是对具体的任意列进行条件筛选,两者条件筛选的阶段不同

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Win10 录屏秘籍大公开:从新手到高手的进阶之路

之前因为某些原因不方便到客户那里进行软件培训&#xff0c;我们就发现录屏讲解供客户随时查看的方式好像更有效果。这次我就介绍一些能够实现win10怎么录屏操作的工具讲解。 1.福昕录屏大师 链接&#xff1a;www.foxitsoftware.cn/REC/ 这个工具是一款专业的电脑录屏软件&a…

【三大运营商】大数据平台体系架构【顶层规划设计】

在国内运营商&#xff08;如中国移动、中国联通、中国电信&#xff09;的大数据平台建设中&#xff0c;顶层规划设计至关重要。以下是针对三大运营商为例【如电信】的大数据平台体系架构的顶层规划设计方案&#xff0c;涵盖整体架构、关键组件、数据管理、应用场景等方面。 1. …

2023年全国研究生数学建模竞赛华为杯B题DFT类矩阵的整数分解逼近求解全过程文档及程序

2023年全国研究生数学建模竞赛华为杯 B题 DFT类矩阵的整数分解逼近 原题再现&#xff1a; 一、问题背景   离散傅里叶变换&#xff08;Discrete Fourier Transform&#xff0c;DFT&#xff09;作为一种基本工具广泛应用于工程、科学以及数学领域。例如&#xff0c;通信信号…

react 基础语法

前置知识 类的回顾 通过class关键字定义一个类 类名首字母大写 class类有constructor构造器 new 一个类得到一个实例 类还有方法&#xff0c;该方法也会在其原型上 static静态数据&#xff0c;访问静态属性通过 类名.id getter和setter getter&#xff1a;定义一个属性&…

kubernetes存储之GlusterFS(GlusterFS for Kubernetes Storage)

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:Linux运维老纪的首页…

Agent Zero

文章目录 一、关于 Agent Zero现在有了UI&#xff1a;关键概念1、General-purpose 助理2、计算机作为工具3、多智能体合作4、完全可定制和可扩展5、沟通是关键 不错的功能记住已知问题理想的环境 二、Setup - 如何在Windows和MacOS上安装Agent Zero提醒&#xff1a;1、安装Cond…

Tiny-universe学习笔记1:Qwen-blog

本文是参与Datawhale Tiny-universe组队学习的第一篇学习笔记&#xff0c;参考链接&#xff1a;https://github.com/datawhalechina/tiny-universe Tiny-universe学习笔记1&#xff1a;Qwen-blog Qwen整体架构与Llama2类似&#xff0c;具体如下图所示&#xff1a; 其中&#…

深度学习笔记(8)预训练模型

深度学习笔记&#xff08;8&#xff09;预训练模型 文章目录 深度学习笔记&#xff08;8&#xff09;预训练模型一、预训练模型构建一、微调模型&#xff0c;训练自己的数据1.导入数据集2.数据集处理方法3.完形填空训练 使用分词器将文本转换为模型的输入格式参数 return_tenso…

Java | Leetcode Java题解之第417题太平洋大西洋水流问题

题目&#xff1a; 题解&#xff1a; class Solution {static int[][] dirs {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int[][] heights;int m, n;public List<List<Integer>> pacificAtlantic(int[][] heights) {this.heights heights;this.m heights.length;this.n…

【JSrpc破解前端加密问题】

目录 一、背景 二、项目介绍 三、JSrpc 处理前端加密步骤 一、背景 解决日常渗透测试、红蓝对抗中的前端密码加密问题&#xff0c;让你的爆破更加丝滑&#xff1b;降低js逆向加密的难度&#xff0c;降低前端加密逻辑分析工作量和难度。 二、项目介绍 运行服务器程序和js脚本…

springCloud(一)注册中心

1.Eureka 要是user-service服务有多个&#xff0c;order-service该怎么调用&#xff1f; 这就需要用到 注册中心 了 。 1.1 搭建Eureka服务 1. pom引入依赖 <dependencies><!--eureka服务端--><dependency><groupId>org.springframework.cloud</gr…

线程局部变量

开发线程的步骤 为什么要学 ThreadLocal 就是为了防止开发随意选库&#xff0c;设置线程局部变量 因为初始化随着项目启动-创建了连接池&#xff0c;但目前getinfo和login都是走从库&#xff0c;没有分开 所以在service层方法运行时&#xff0c;用ReqAop代码提前算出此方法走…

将有序数组——>二叉搜索树

给你一个整数数组 nums &#xff0c;其中元素已经按 升序 排列&#xff0c;请你将其转换为一棵平衡二叉搜索树。 示例 1&#xff1a; 输入&#xff1a;nums [-10,-3,0,5,9] 输出&#xff1a;[0,-3,9,-10,null,5] 解释&#xff1a;[0,-10,5,null,-3,null,9] 也将被视为正确答案…

Modbus_RTU和Modbus库

目录 一.Modbus_RTU 1. 与Modbus TCP的区别 2. Modbus RTU特点 3. Modbus RTU协议格式 4. 报文详解 5. 代码实现RTU通信 1. 打开模拟的RTU从机 2. linux端使用代码实现和串口连接 2.1. 框架搭建 2.2 代码 二.Modbus库 1.库函数 一.Modbus_RTU 1. 与Modbus T…

C++初阶学习第六弹------标准库中的string类

目录 一.标准库中的string类 二.string的常用接口函数 2.1string类对象的构造 2.2 string的容量操作 2.3 string类的访问与遍历 2.4 string类对象的修改 2.5 string类常用的非成员函数 三、总结 一.标准库中的string类 可以简单理解成把string类理解为变长的字符数组&#x…

c++234继承

#include<iostream> using namespace std;//public 修饰的成员便俩个和方法都能使用 //protected&#xff1a;类的内部 在继承的子类中可使用 class Parents { public:int a;//名字 protected:int b;//密码 private:int c;//情人public:void printT(){cout << &quo…

C:字符串函数(完)-学习笔记

目录 前言&#xff1a; 1、strstr 1.1 strstr的使用 4.2 strstr的模拟实现 5、strtok 5.1 strtok函数的介绍 5.2 strtok函数的使用 6、strerror 前言&#xff1a; 这篇文章将介绍strstr函数&#xff0c;strtok函数&#xff0c;strerror函数 1、strstr 1.1 strstr的使用…

RabbitMQ 高级特性——持久化

文章目录 前言持久化交换机持久化队列持久化消息持久化 前言 前面我们学习了 RabbitMQ 的高级特性——消息确认&#xff0c;消息确认可以保证消息传输过程的稳定性&#xff0c;但是在保证了消息传输过程的稳定性之后&#xff0c;还存在着其他的问题&#xff0c;我们都知道消息…

Linux内核结构

Linux内核结构 文章目录 Linux内核结构一、Linux内核结构介绍1.1 总体结构&#xff1a;1.2 Linux内核结构框图&#xff1a; 二、图解Linux系统架构三、shell3.1 shell的含义&#xff1a;3.2 shell的作用&#xff1a;3.3 shell的类型&#xff1a;3.4 shell的使用&#xff1a;3.5…

安泰电压放大器设计方法是什么样的

电压放大器是电子领域中常用的设备&#xff0c;用于将低电压信号放大成高电压信号。电压放大器在信号处理、通信系统、仪器测量、控制系统、医疗设备和研究和实验室等领域都有着广泛的应用。 电压放大器的设计方法主要包括选择合适的放大器拓扑结构、选择适当的放大器参数以及进…