mysql行增删改和单表查询

本文介绍了MySQL中插入、更新、删除数据的基本语法,强调了`UPDATE`不加`WHERE`条件会修改所有数据,`DELETE`不会重置自增列,通常使用`TRUNCATE`来清空表。还详细讲解了单表查询的步骤和各种查询关键字的执行顺序,如`FROM`、`WHERE`、`GROUP BY`、`HAVING`、`SELECT`、`DISTINCT`、`ORDER BY`和`LIMIT`的使用方法,并提到了`ONLY_FULL_GROUP_BY`模式的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

插入 insert
insert into 表名(字段1,字段2,字段3,……,字段n) values(值1,值2,值3,……,值n);
如果不指定字段则需要在values处输入全部字段信息

insert into book(id,name) values(1,"book1");

将查询出的数据插入指定表
insert into 表名1(字段1,字段2,字段3,……,字段n) select(字段1,字段2,字段3,……,字段n) from 表名2(where 字段 = condition);

更新update
update 表名 set 字段1 = 值1,字段2 = 值2 where condition;
注意如果后面不指定where条件,会修改这个字段所有数据

update book set name = "book2" where id = 1;

删除数据delete
delete from 表名 where condition;
注意如果不加where,会将表内所有数据删除

delete from book where id = 1;

delete的特性是虽然删除了数据但是自增列信息还会保存,即如果删除了一条数据id=1(这里id为自增列),再插入一条新数据的话id会自动置为2。
所以,为了同时让自增列重置,我们一般会用truncate清空表。


表的查询

单表查询
select distinct 字段1,字段2,字段3,……,字段n from 库名.表名;

					where condition;#从表中找符合条件的数据记录
					group by field;#分组
					having;#过滤后执行select后面的字段筛选
					order by field;#将结果按照后面的字段进行排序
					limit;#限制显示条数

关键字的执行优先级
1.找到表:from
2.拿着where指定的约束条件,去文件/表中取出一条条记录。
3.将取出的一条条记录进行分组:group by
4.将分组的结果进行having过滤
5.执行select
6去重:distinct
7.将结果按照条件排序:order by
8限制结果的显示条数:limite


例子对于关键字的演示

场景:

 insert into emp(name,post,salary) values
 ("gaga","tec",10000),
 ("xjj","sale",8000),
 ("mb","tec",9000),
 ("cjj","tec",9500),
 ("brownie","sale",6000),
 ("gagaga","manager",15000);

distinct

select post from emp;

select distinct post from emp;
select distinct post,id from emp;

如果写入两个字段的话,只有两个字段同时重复才算重复。

select

select name,salary from emp;

select salary*12 from emp;-- 可以返回四则运算后的值

concat
指定数据显示格式并以一列数据显示

select concat("姓名:",name,"年薪",salary*12) from emp;

select concat("姓名",name,"年薪",salary*12) as employeeinfo,post from emp;-- 可以指定行名

where
加入条件

select * from emp where id >= 2;-- 支持运算符

select * from emp where id >2 and salary >8000;-- 支持多条件查询

select * from emp where id between 10 and 15;-- between代表在谁和谁之间,相当于大于等于10小于等于15

select * from emp where id in(1,3,6);-- 提取id为1,3,6的数据

select * from emp where name like '%jj';-- 模糊匹配,%代表多个,_代表一个

SELECT name from emp WHERE not id < 2;-- 可以取反,not in

group by
1.分组发生在where之后,是将where取出之后的数据分组。
2.将所有记录按照某个相同字段进行分组。
3.分组目的:有时需要对分组数据进行操作。

select * from emp group by post;-- 无意义,仅分组,会把每一字段的第一条数据显示出来

select count(id),post from emp group by post;-- 可以加内置函数计算分组内数据

为了避免这种无意义的分组,可以设置sql模式为ONLY_FULL_GROUP_BY
设置这一模式后,select后的内容只能是group by后面的字段或内置函数。

set global sql_mode = "STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY";
select max(salary) from emp group by post;

select max(salary) from emp group by post,id;-- 可以用多个字段分组,但是只有post和id都相同才算一组

having
group by后筛选

select post,max(salary) from emp group by post having max(salary)>8000;

having后可以跟聚合函数,where后不可以。
order by

select * from emp order by salary;

select * from emp order by salary desc;-- 降序,默认升序

select * from emp order by salary asc, id desc;-- 多条件排序,salary一样再排id

limit

select * from emp limit 3;-- 只显示前3条
select * from emp limit 0,5;-- 从第1条开始拿5条

正则表达式

select * from emp where name regexp '.jj';
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值