欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > 数据库 >内容正文

数据库

mysql增删改查扩展_MySQL(增删改查补充)

发布时间:2025/3/17 数据库 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 mysql增删改查扩展_MySQL(增删改查补充) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

SQL语句数据行操作补充

create table tb12(

id int auto_increment primary key,

name varchar(32),

age int

)engine=innodb default charset=utf8;

insert into tb11(name,age) values('alex',12);

insert into tb11(name,age) values('alex',12),('root',18);     #同时增加多条

insert into tb12(name,age) select name,age from tb11;         #将tb11表整个插入tb12

delete from tb12;

delete from tb12 where id !=2

delete from tb12 where id =2

delete from tb12 where id > 2

delete from tb12 where id >=2

delete from tb12 where id >=2 or name='alex'

update tb12 set name='alex' where id>12 and name='xx'      #条件

update tb12 set name='alex',age=19 where id>12 and name='xx'

select * from tb12;

select id,name from tb12;

select id,name from tb12 where id > 10 or name ='xxx';

select id,name as cname from tb12 where id > 10 or name ='xxx';    #将name取别名显示

select name,age,11 from tb12;  #第三列全为11

其他:

select * from tb12 where id != 1

select * from tb12 where id in (1,5,12);  # 1 or 5 or 12

select * from tb12 where id not in (1,5,12);

select * from tb12 where id in (select id from tb11)  #范围克重另一张表中选择

select * from tb12 where id between 5 and 12;  #闭区间

通配符:

select * from tb12 where name like "a%" #以a开头的        %a以a结尾的

select * from tb12 where name like "a_" #a后边只有一个位置

分页:

select * from tb12 limit 10;    #分页显示

select * from tb12 limit 0,10;

select * from tb12 limit 10,10;

select * from tb12 limit 20,10;   #起始位置,和显示数量    从20个开始,往后显示10个

select * from tb12 limit 10 offset 20;

从第20行开始读取,读取10行;

排序:

select * from tb12 order by id desc; 大到小

select * from tb12 order by id asc;  小到大

select * from tb12 order by age desc,id desc;

取后10条数据    id从大到小排后取前十个

select * from tb12 order by id desc limit 10;

补充:

左右连表: join

上下连表: union

# 自动去重

select id,name from tb1

union

select num,sname from tb2

# 不去重

select sid,sname from student

UNION ALL

select sid,sname from student

总结

以上是生活随笔为你收集整理的mysql增删改查扩展_MySQL(增删改查补充)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。