MySQL之表的增删查改(1)

目录

一、插入数据

1、单行数据 + 全列插入

2、多行数据 + 指定列插入

3、插入否则更新

4、替换

二、读取

1、select列

2、where条件

3、结果排序

4、筛选分页结果

一、插入数据

首先创建一张表

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) unique
    -> );

1、单行数据 + 全列插入

插入两条记录,value_list 数量必须和定义表的列的数量及顺序一致

mysql> insert into students values(100,10000,'唐三',null);
Query OK, 1 row affected (0.00 sec)

mysql> insert into students values(101,10001,'唐三藏','12345');
Query OK, 1 row affected (0.01 sec)

2、多行数据 + 指定列插入

插入两条记录,value_list 数量必须和指定列数量及顺序一致
mysql> insert into students (id,sn,name) values (102,10002,'孙悟空'),
(103,10003,'孙仲谋');

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

3、插入否则更新

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

mysql> insert into students (id,sn,name) values (100,10010,'唐唐');
ERROR 1062 (23000): Duplicate entry '100' for key 'PRIMARY'

mysql> insert into students (id,sn,name) values (104,10001,'曹阿瞒');
ERROR 1062 (23000): Duplicate entry '10001' for key 'sn'

此时我们可以选择性的进行更新操作,

语法:

INSERT ... ON DUPLICATE KEY UPDATE
column = value [, column = value] ...
//ON DUPLICATE KEY 当发生重复key的时候
mysql> insert into students (id,sn,name) values (100,10010,'唐唐') 
on duplicate key update sn=10010,name='唐唐';

Query OK, 2 rows affected (0.00 sec)
说明:
0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等;
1 row affected: 表中没有冲突数据,数据被插入;
2 row affected: 表中有冲突数据,并且数据已经被更新;
注意不要和其他行的主键或者唯一键冲突;

可以通过 MySQL 函数获取受到影响的数据行数

4、替换

主键 或者 唯一键 没有冲突,则直接插入; 主键 或者 唯一键 如果冲突,则删除后再插入
mysql> replace into students (sn,name) values(10010,'曹阿瞒');
Query OK, 2 rows affected (0.00 sec)
 说明: 
1 row affected: 表中没有冲突数据,数据被插入
 2 row affected: 表中有冲突数据,删除后重新插入

二、读取

创建表

mysql> CREATE TABLE exam_result (
    -> id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    -> name VARCHAR(20) NOT NULL COMMENT '同学姓名',
    -> chinese float DEFAULT 0.0 COMMENT '语文成绩',
    -> math float DEFAULT 0.0 COMMENT '数学成绩',
    -> english float DEFAULT 0.0 COMMENT '英语成绩'
    -> );
Query OK, 0 rows affected (0.03 sec)

插入数据

mysql> insert into exam_result(name,chinese,math,english) values
    -> ('凯文',67,98,56),
    -> ('琪亚娜',87,78,77),
    -> ('芽衣',88,98,90),
    -> ('板鸭',82,84,67),
    -> ('瓦尔特',55,85,45),
    -> ('温蒂',70,73,78),
    -> ('劫哥',75,65,30);
Query OK, 7 rows affected (0.01 sec)
Records: 7  Duplicates: 0  Warnings: 0

1、select列

(1)全列查询

说明:

通常情况下不建议使用 * 进行全列查询
——查询的列越多,意味着需要传输的数据量越大;
——可能会影响到索引的使用。(索引待后面课程讲解)
SELECT * FROM exam_result;

(2)指定列查询

 select id,name from exam_result;

(3)查询字段为表达式

select id,name, 100 from exam_result;

select id,name, english+10 from exam_result;

select id,name,chinese+math+english from exam_result;

(4)为查询结果指定别名

SELECT column [AS] alias_name [...] FROM table_name;
mysql> select id,name 名字,chinese+math+english 总分 from exam_result;

5(结果去重)

mysql> select distinct math from exam_result;

2、where条件

比较运算符:

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

mysql> select name,english from exam_result where  english<60;

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

mysql> select name,chinese from exam_result where chinese>=80 and chinese<=90;

mysql> select name,chinese from exam_result where chinese between 80 and 90;

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

mysql> select name,math from exam_result where
    -> math=58
    -> or math=59
    -> or math=98
    -> or math=99;

mysql> select name,math from exam_result where math in(58,59,98,99);

(4)找出琪某某和凯某

mysql> select name,math from exam_result where name like '琪%';
% 匹配任意多个(包括0个)任意字符

mysql> select name,math from exam_result where name like '凯_';
_ 匹配严格的一个任意字符

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

mysql> select name,chinese,english from exam_result where chinese>english;

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

mysql> select name, (math+chinese+english) from exam_result 
where (math+chinese+english)<200;

mysql> select name, (math+chinese+english) 总分  from exam_result where (math+chinese+english)<200;


select name, (math+chinese+english) 总分  from exam_result where 总分<200;
ERROR 1054 (42S22): Unknown column '总分' in 'where clause'

分析:在 SQL 语言中,where 和 select 的执行顺序是这样的:

  • 首先,根据 from 子句确定要查询的表或视图。
  • 其次,根据 where 子句过滤出满足条件的记录。
  • 最后,根据 select 子句选择要显示的字段和计算的结果。

因此,where 子句是在 select 子句之前执行的,这意味着 where 子句中不能使用 select 子句中定义的别名或计算字段。

(7)语文成绩好于80,并且不是琪某某和芽某

mysql> select name,chinese from exam_result where chinese>80 
and name not like '琪%' and name not like '芽%';

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

mysql> select name,math,chinese,english, math+english+chinese 总分
    -> from exam_result where
    -> name like '温_' or(
    -> chinese+math+english>200 and chinese<math and english>80
    -> );

(9)null的查询


mysql> select name,qq from students where qq is not null;

mysql> SELECT NULL <=> NULL, NULL <=> 1, NULL <=> 0;
1 row in set (0.00 sec)

mysql> select null<=>null,null<=>1,null<=>0;
1 row in set (0.00 sec)

3、结果排序

语法:

ASC 为升序(从小到大)
DESC 为降序(从大到小)
默认为 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
注意:没有 ORDER BY 子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序

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

mysql> select name ,math from exam_result order by math asc;

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

        多字段排序,排序优先级随书写顺序

mysql> select name,math,english,chinese from exam_result 
    -> order by math desc,english,chinese;

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

mysql> select name ,math+english+chinese from exam_result
    -> order by math+english+chinese desc;

(4)查询姓琪亚娜,芽衣和板鸭的数学成绩,结果按数学成绩由高到低显示

mysql> select name,math from exam_result where name like '琪%' 
or name like '芽%' or name like '板%' order by math desc;

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;
建议:对未知表进行查询时,最好加一条 LIMIT 1 ,避免因为表中数据过大,查询全表数据导致数据库卡死
示例: id 进行分页,每页 3 条记录,分别显示 第 1 2 3
mysql> select id,name,math+english+chinese 总分 
from exam_result order by id limit 3 offset 0;

mysql> select id,name,math+english+chinese 总分 
from exam_result order by id limit 3 offset 3;

mysql> select id,name,math+english+chinese 总分 
from exam_result order by id limit 3 offset 6;

如果结果不足 3 个,不会有影响。


                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ke vin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值