【简单易懂】MySQL表的增删改查(初阶)

目录

1.注释(CRUD)

2.新增(Create)

3.查询(Retrieve)

3.1.全列查询

3.2.指定列查询

3.3.带表达式的查找

3.4.带别名的查找

3.5.查找结果去重

3.6.排序

3.7.条件查询

3.8.分页查询

4.修改

5.删除


1.注释(CRUD)

注释:在 SQL 中可以使用 “-- 空格 + 描述 来表示注释说明
CRUD:即增加 (Create) 、查询 (Retrieve) 、更新 (Update) 、删除 (Delete) 四个单词的首字母缩写。

2.新增(Create)

新增单条语句:

insert into 表名(列名) values (    );

新增多条语句:

每条语句使用 逗号 隔开

insert into 表名(列名) values (  ), (  ), (  );

 例如我要在 books 表中新增两本书,代码如下

insert into books (name, author, price, class) values
	('python','wnagwu',30,'jiaocai'),
    ('gaoshu', 'wulin', 35, 'jiaocai');

3.查询(Retrieve)

3.1.全列查询

select * from 表名

3.2.指定列查询

select 列名,列名.... from 表名 

 例如指定查找表中列名为 name、price 的内容

select name, price from books;

3.3.带表达式的查找

select  表达式 from 表名 

表达式是列和列之间的计算,把表中的每一行里面的对应列,进行计算

例如将价格 price + 10 然后显示出来。

注意:这里的 + 10 只是针对查询结果显示,并不会改变数据库里面的数值

select name, price + 10 from books;

 效果如下

3.4.带别名的查找

给查询结果临时表的列起别名

select  表达式  as 别名  from 表名 

 例如将成绩表的 chinese 、math 、 english 做为一个表达式,然后起个别名 total 显示出来

select name, chinese + math + english as total from exam;

3.5.查找结果去重

根据查询结果,去掉重复的行

select distinct  列名 from 表名 

 例如去掉数学成绩相同的行,只剩下一行

select distinct  math from exam; 

3.6.排序

select 列名 from 表名 order by 列名 asc/desc, 列名 asc/desc; 

asc 表示升序,默认是升序

指定多个列排序的时候,以第一列为主,第二列其次 ..... 当第一列的值相同,才比较第二列。如果第一列已经分出大小关系,就不需比较后面的列了

 例如按照语文的成绩升序来进行显示

如果成绩里面有 NULL 值,NULL值就是最小的,排在最前面。

select * from exam order by chinese asc; 

 排序也可以利用表达式或者别名来进行排序

select 列名,表达式 from 表名 order by 表达式; 

select name,chinese + math + english as total from exam 
order by chinese + math + english; 

 在指定多个列排序的时候,具有明确优先级的,如果第一列成绩有一样的,就根据第二列来排序

select name, chinese, math, from exam order by chinese, math;

3.7.条件查询

在select 的后面加上一个 where 字句,后面跟上一个具体的筛选条件

select 列名 from 表名 where 条件;

查询结果就会把满足条件的记录保留,把不满足条件的记录给过滤掉 

其中 <=> 用于在空值NULL中比较相等,其它用 = 比较相等。

如 NULL <=> NULL , 返回结果为 true ,

如果使用错误,如 NULL = NULL ,返回 false。

3.7.1针对列

针对 列 条件查询,将 math 成绩大于80 的展示出来。

select name, math from exam where math > 80;

3.7.2 针对行的内容 

 针对行之间的内容进行比较,不测及行与行之间的比较

展示 chinese > math

 展示chinese < math

 展示总分小于200

 3.7.3  使用 and 与 or 条件查询

and 的优先级高于 or 。在同时使用时,如果需要先计算or的话需要使用小括号 ()。

例如找出 math > 80 and english > 80,  或者 chinese > 50 的

select * from exam where chinese > 50 or math > 80 and english > 80;

 3.7.4 bewteen... and...

使用 bewteen ... and.. , 前后都是闭区间,即[   ,    ]。

例如查询 math 成绩在 [ 85, 90] 的

select name, math from exam where math between 85 and 90;

 3.7.5 使用()查询

只要符合() 内的任意数值都可以被查询出来,();里的内容就相当于 or

例如( 50, 60, 80, 85) 就相当于50or 60 or 80 or 85

select * from exam where math in (50, 60, 80, 85);

 3.7.6 通配符查询 like

% 代表任意字符 (也包括 0 字符)

例如找出 name 里面有 li某某的,就可以使用 li%;

select * from exam where name like 'li%';

_ 代表任意一个字符

例如找出 math 开头为8 的,就可以写成 like '8___'; 因为成绩是三位有效数字加一个点,所以需要三个‘_’

select * from exam where math like '8___';

3.8.分页查询

select 列名 from 表名 limit N offset M; 

从第M条记录开始,一共返回N个记录 

例如查询表中的三条记录,limit 3;

select * from exam limit 3;

从第三条开始往下数的两条,limit 2 offset 3;

select * from exam limit 2 offset 3;

4.修改

update 表名 set 列名 = 值 , 列名 = 值 ..... where 条件

其中修改是针对 满足条件 来进行修改的,update 是会修改数据库服务器上面的原始数据

例如 将表中 name = ‘lisi’ 的chinese改为75,math 改为80;

update exam set chinese = 75, math = 80 where name = 'lisi';

将原本所有同学的 chinese 更新为原来的两倍,

update exam set chinese = chinese / 2;

5.删除

删除条件的内容,如果没有 where , 则会将整个表的数据删除 ,但是还好留有一个空表

delect from 表名 where 条件

drop 则是将表和数据都删除,谨慎使用

drop table 

猜你喜欢

转载自blog.csdn.net/m0_60494863/article/details/124567930