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

news2024/9/20 17:32:55

目录

一: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.coloradmin.cn/o/2149524.html

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

相关文章

助力企业降低成本,ByteHouse打造新一代“弹性”云数仓

更多技术交流、求职机会&#xff0c;欢迎关注字节跳动数据平台微信公众号&#xff0c;回复【1】进入官方交流群 在当今的互联网应用中&#xff0c;业务流量往往具有很大的不确定性。 例如&#xff0c;电商平台在 “618”“双 11” 等促销活动期间&#xff0c;访问量会呈爆发式增…

计算机毕业论文题目之基于Web技术B/S结构的新生管理系统包含报道,寝室宿舍,缴费学费,数据统计分析汇总等功能的源代码下载

为了满足功能需求&#xff0c;我们将设计并实现一个基于Web技术的B/S架构下的新生管理系统。本系统旨在通过前端与后端分离的设计模式&#xff0c;为用户提供简洁、高效的交互体验&#xff0c;并确保数据的安全性和系统的可扩展性。下面将从系统架构、功能模块以及技术选型三个…

LeetCode[中等] 142. 环形链表 II

给定一个链表的头节点 head &#xff0c;返回链表开始入环的第一个节点。 如果链表无环&#xff0c;则返回 null。 如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到达&#xff0c;则链表中存在环。 为了表示给定链表中的环&#xff0c;评测系统内部使用整…

SwiftData 共享数据库在 App 中的改变无法被 Widgets 感知的原因和解决

0. 问题现象 我们 watchOS 中的 App 和 Widgets 共享同一个 SwiftData 底层数据库&#xff0c;但是在 App 中对数据库所做的更改并不能被 Widgets 所感知。换句话说&#xff0c;App 更新了数据但在 Widgets 中却看不到。 如上图所示&#xff1a;我们的 App 在切换至后台之前会…

你是不是分不清哪些字体是商用,哪些非商用?快来看,免得莫名其妙负债。

前言 最近发现有好多小伙伴在做PPT的时候&#xff0c;都有一个很不好的习惯&#xff1a;没有调整好字体。 这里说的没有调整好字体的意思是&#xff1a;在一些公开发布的内容上使用一些可能造成侵权的字体。 字体侵权‌的后果相当严重。轻者可能面临法律纠纷&#xff0c;重者…

软件开发团队时间管理的5大技巧

软件开发团队运用时间管理技巧&#xff0c;有助于提升项目效率&#xff0c;确保任务按时完成&#xff0c;减少资源浪费&#xff0c;节约开发时间&#xff0c;增强团队协作&#xff0c;最终有利于项目成功交付。如果开发团队不采取时间管理技巧&#xff0c;可能导致项目延期、资…

如何搭建客户服务知识库?五项基本方法让你业务增长100%

在竞争激烈的市场环境中&#xff0c;优质的客户服务已成为企业脱颖而出的关键。而一个高效、全面的客户服务知识库&#xff0c;不仅能够提升客户满意度&#xff0c;还能显著降低客服团队的工作负担&#xff0c;促进业务的稳健增长。本文将介绍五项基本方法&#xff0c;帮助你搭…

SpringBoot Admin调整类的日志级别

进入 SpringBoot Admin &#xff0c;通过服务名称&#xff0c; 找到服务后。 点击 “日志” – “日志配置” &#xff0c;输入类名&#xff0c;即可调整 这个类的日志级别。

Python模块和包:标准库模块(os, sys, datetime, math等)②

文章目录 一、os 模块1.1 获取当前工作目录1.2 列出目录内容1.3 创建和删除目录1.4 文件和目录操作 二、sys 模块2.1 获取命令行参数2.2 退出程序2.3 获取 Python 版本信息 三、datetime 模块3.1 获取当前日期和时间3.2 日期和时间的格式化3.3 日期和时间的运算 四、math 模块4…

.NET常见的几种项目架构模式,你知道几种?

前言 项目架构模式在软件开发中扮演着至关重要的角色&#xff0c;它们为开发者提供了一套组织和管理代码的指导原则&#xff0c;以提高软件的可维护性、可扩展性、可重用性和可测试性。 假如你有其他的项目架构模式推荐&#xff0c;欢迎在文末留言&#x1f91e;&#xff01;&am…

flutter遇到问题及解决方案

目录 1、easy_refresh相关问题 2、 父子作用域关联问题 3. 刘海屏底部安全距离 4. 了解保证金弹窗 iOS端闪退 &#xff08;待优化&#xff09; 5. loading无法消失 6. dialog蒙版问题 7. 倒计时优化 8. scrollController.offset报错 9. 断点不走 10.我的出价报红 11…

4、(PCT)Point Cloud Transformer

4、&#xff08;PCT&#xff09;Point Cloud Transformer 论文链接&#xff1a;PCT论文链接 本篇论文介绍Transformer在3D点云领域的应用&#xff0c;Transformer在NLP领域和图像处理领域都得到了广泛的应用&#xff0c;特别是近年来在图像领域的应用&#xff0c;本篇论文主要…

希亦超声波清洗机值得购买吗?百元清洁技术之王,大揭秘!

现代社会的高速发展&#xff0c;很多人由于工作繁忙的原因&#xff0c;根本没有时间去清洗自己的日常物品&#xff0c;要知道这些日常物品堆积灰尘之后是很容易就滋生细菌的&#xff0c;并且还会对人体的健康造成一定的危害&#xff01;这个时候很多人就会选择购买一台超声波清…

耐高温滑环的应用场景及市场前景分析

耐高温滑环是一种重要的电气连接装置&#xff0c;广泛应用于需要传递电力和信号的旋转设备中。随着工业技术的发展&#xff0c;对耐高温滑环的需求不断增加&#xff0c;尤其是在极端温度环境下的应用场合&#xff0c;耐高温滑环展现出其独特的优势。 耐高温滑环在工业自动化领…

全国网安众测招募计划启动啦,欢迎加入~

在数字化时代&#xff0c;网络安全已成为维护社会稳定、促进经济发展的基石。为了积极响应国家关于加强网络安全工作的号召&#xff0c;确保某区域关键信息系统的稳固运行&#xff0c;我们特此启动一项网络安全众测活动。该活动旨在通过汇聚业界有经验的网络安全攻防人才&#…

【小程序 - 大智慧】深入微信小程序的渲染周期

目录 前言应用生命周期页面的生命周期组件的生命周期渲染顺序页面路由运行机制更新机制同步更新异步更新 前言 跟 Vue、React 框架一样&#xff0c;微信小程序框架也存在生命周期&#xff0c;实质也是一堆会在特定时期执行的函数。 小程序中&#xff0c;生命周期主要分成了三…

使用 VSCode 在 Python 中创建项目环境

了解如何管理 Python 项目的不同环境&#xff0c;欢迎来到雲闪世界。 添加图片注释&#xff0c;不超过 140 字&#xff08;可选&#xff09; 介绍 创建数据科学项目非常简单。如今&#xff0c;有了众多资源&#xff0c;您只需选择开发工具并启动项目即可。 除了多个人工智能机…

24.9.16数据结构|平衡二叉树

一、理解逻辑 平衡二叉是有限制的二叉搜索树&#xff0c;满足平衡因子绝对值小于1的二叉搜索树是平衡二叉树。 平衡指的是树的左右两边的节点左右高度平衡&#xff0c;要求平衡因子处于规定范围 平衡因子&#xff1a;该节点的左高度-右高度&#xff0c;绝对值小于1 如何平衡化&…

2024年9月20日历史上的今天大事件早读

公元前480年9月20日 希腊人在爱琴海萨拉米海战中击败了波斯人 383年9月20日 发生“淝水之战” 1013年9月20日 《君臣事迹》书成&#xff0c;赐名《册府元龟》 1519年9月20日 葡萄牙航海家麦哲伦环球航行 1644年9月20日 清顺治帝驾车由盛京出发&#xff0c;迁都北平&#xf…

在SpringCloud中实现服务熔断与降级,保障系统稳定性

在分布式系统中&#xff0c;微服务架构的应用越来越受欢迎。然而&#xff0c;由于各个微服务之间的依赖关系和网络通信的不稳定性&#xff0c;一个不稳定的服务可能会对整个系统产生连锁反应&#xff0c;导致系统崩溃。为了保障系统的稳定性&#xff0c;我们需要一种机制来处理…