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: 0mysql> 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 为降序(从大到小)
-- 默认为 ascselect ... 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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 -ADatabase 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.xdnf.cn/news/142648.html

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

相关文章

数据结构之栈(python)

栈&#xff08;顺序栈与链栈&#xff09; 1.栈存储结构1.1栈的基本介绍1.2进栈和出栈1.3栈的具体实现1.4栈的应用例一例二例三 2.顺序栈及基本操作&#xff08;包含入栈和出栈&#xff09;2.1顺序栈的基础介绍2.2顺序栈元素入栈2.3顺序栈元素出栈2.4顺序栈的表示和实现 3.链栈及…

吐血整理资料后,测试面试相关的资料大全

最近假期收集资料的过程真的让我吐血。 在博客上随便搜一点资料&#xff0c;好多只能看一般&#xff0c;或者打着分享资源&#xff0c;但实际上并不是了。而且大部分资料就是网上找的&#xff0c;恰饭不好评价&#xff0c;但体验真的好差啊&#xff01; 我搜集了很多面试资料…

流程图怎么画?3个好用的在线流程图软件推荐,绘图没烦恼!

目录 什么是流程图&#xff1f; 为什么需要使用流程图&#xff1f; 流程图中各种图形的含义 如何制作流程图&#xff1f; 小结&#xff1a;流程图如何制作&#xff1f; 流程图是表达工作流程或者系统操作过程的有效工具&#xff0c;被广泛应用于各个行业和领域。…

Centos7环境下Hive的安装

Centos7环境下Hive的安装 前言一、安装Hive1.1 下载并解压1.2 配置环境变量1.3 修改配置1. hive-env.sh2. hive-site.xml 1.4 拷贝数据库驱动1.5 初始化元数据库报错 1.6 安装MySQL1.7 启动 二、HiveServer2/beeline2.1 修改Hadoop配置2.2 修改Hive配置2.2 启动hiveserver22.3 …

2024/9/16 dataloader、tensorboard、transform

一、pytorch两大法宝元素 假设有一个名为pytorch的包 dir()&#xff1a;用于打开包&#xff0c;看里面的内容 help():用于查看具体的内容的用处 二、python文件&#xff0c;python控制台和jupyter的使用对比 三、pytorch读取数据 pytorch读取数据主要涉及到两个类&#xff1…

基于 PyQt5 和 OpenCV 进行图像处理操作的GUI工具初版

为了实现一个基于 PyQt5 和 OpenCV 的图形用户界面&#xff08;GUI&#xff09;&#xff0c;要求如下&#xff1a; 左边显示加载的图片。 中间提供各种对图片进行处理的操作方法&#xff08;如灰度化、模糊处理等&#xff09;。 右边显示处理后的效果图。 接下来我将详细讲解如…

【C语言零基础入门篇 - 5】:探索C语言中丰富多样的选择和循环结构

文章目录 选择分支结构if选择结构if...else...选择结构if...else if...else 嵌套选择结构switch多分支结构 循环结构for 循环while 循环do … while 循环 跳转语句 选择分支结构 if选择结构 基本格式&#xff1a;if&#xff08;表达式1)语句1; 功能&#xff1a;如果表达式1…

坚持的力量--完成向CSDN迁移500篇技术文章阶段小记-以此自勉

前言&#xff1a;本文为迁移前的博客中的文章《坚持的力量-写给第1000篇文章笔记》发表于 publish:May 14, 2021 -Friday&#xff0c;作为原个人博客中累计写满1000篇文章的总结和鼓励。因在向CSDN迁移文章的过程中进行了一些文章合并等调整&#xff0c;总文数大量下降&#xf…

【建设方案】固定资产信息系统建设方案(功能清单列表2024word原件)

固定资产管理系统需求要点&#xff1a; 1. 实现公司内部固定资产管理全生命周期管理&#xff0c;包括资产采购、资产入库、资产领用、资产借用、资产归还、资产报废、资产维修、资产调拨等全过程管理。 2. 可实现集团内部固定资产盘点管理&#xff0c;包括盘点计划、盘点查询等…

Python数据分析与可视化的全面指南

目录 引言 一、Python数据分析基础 1. 为什么选择Python进行数据分析&#xff1f; 2. 常用的数据分析库 二、环境配置 1. 安装Python 2. 安装Anaconda&#xff08;可选&#xff09; 3. 创建虚拟环境 4. 安装必要的库 三、数据分析流程 四、数据处理与分析 1. 导入库…

【PHP代码审计】PHP基础知识

&#x1f31d;博客主页&#xff1a;菜鸟小羊 &#x1f496;专栏&#xff1a;Linux探索之旅 | 网络安全的神秘世界 | 专接本 | 每天学会一个渗透测试工具 php简介 php是什么&#xff1f; php&#xff08;全称&#xff1a;Hypertext Preprocessor&#xff0c;即超文本预处理器&…

圆环加载效果

效果预览 代码实现 from PyQt5.QtCore import QSize, pyqtProperty, QTimer, Qt, QThread, pyqtSignal from PyQt5.QtGui import QColor, QPainter from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QVBoxLayout, QLabel, QGridLayoutclass Cir…

数据与结构算法平衡二叉树详解叉树--基本概念

平衡二叉树&#xff08;Balanced Binary Tree&#xff09;又被称为AVL树&#xff08;有别于AVL算法&#xff09;&#xff0c;且具有以下性质&#xff1a;它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1&#xff0c;并且左右两个子树都是一棵平衡二叉树。这个方案很好…

图解Redis 01 | 初识Redis

什么是 Redis&#xff1f; Redis 是一种基于内存的数据库&#xff0c;所有的数据读写操作都在内存中完成&#xff0c;因此读写速度非常快。它被广泛应用于缓存、消息队列、分布式锁等场景。 Redis 提供了多种数据类型来支持不同的业务需求&#xff0c;如 String、Hash、List、…

环形数组与单向链表的队列实现(Queue)

什么是队列 队列是一种重要的线性数据结构&#xff0c;具有先进先出&#xff08;FIFO&#xff09;的特性。元素的插入操作称为入队&#xff0c;删除操作称为出队。队列在许多计算机科学应用中非常常见&#xff0c;如任务调度和数据缓冲等。 在实现队列时&#xff0c;可…

路由策略PBR

文章目录 策略路由PBR概述本地流量接口策略 策略路由 策略路由和路由策略的区别&#xff1a; 策略路由可以不按照路由表进行转发路由策略主要控制路由信息的引入、发布和接受等&#xff0c;主要靠 RIB和FIB PBR概述 比路由策略耗资源&#xff0c;直接跳过路由表&#xff0c;数…

Pytorch详解-模型模块(RNN,CNN,FNN,LSTM,GRU,TCN,Transformer)

Pytorch详解-模型模块 Module & parameterModule初认识forward函数 ParameterPytorch中的权重、参数和超参数 Module容器-ContainersSequentialModuleListModuleDictParameterList & ParameterDict 常用网络层LSTM输入和输出 GRUConvolutional Layers卷积层的基本概念常…

IP协议及相关特性

IP协议负责地址管理和路由选择。它的组成为&#xff1a; 接下来我们将对其中较重要的部分进行介绍。 4位版本&#xff1a;这里的四位版本只有两个取值 分别为IPv4和IPv6&#xff0c;这两个额分别为不同的IP协议&#xff0c;但是现在主流的还是IPv4但是近年来IPv6在中国的普及率…

linux系统如何通过进程PID号找到对应的程序在系统中的路径

linux系统如何通过进程PID号找到对应的程序在系统中的路径 首先我们用ps -aux​命令找到对应进程的PID号&#xff0c;比如我这里要得就是xmrig这个进程的PID号 ​​ 通过lsof命令查看对应进程的关联的文件&#xff0c;并找到可执行文件的路径 lsof -p 22785 | grep txt​​ 或…

棉花叶片病害检测数据集

【棉花叶片病害检测数据集】nc: 5 names: [blight, curl, healthy, wilt, wilt_png] 名称&#xff1a;【枯萎病, 卷叶病, 健康&#xff0c;萎蔫病&#xff0c;‘萎蔫病图像’】共3474张&#xff0c;8:1:1比例划分&#xff0c;&#xff08;train;2888张&#xff0c;val&#xff…