增
insert into tb1(name,age) values(‘alex‘,12),(‘egg‘,18);
insert into tb2(name,age) select name,age from tb1;
删
delete from tb1 where id>2 and name=‘alex‘
改
update tb2 set name=‘alex‘,age=19 where id>12 and name=‘xx‘
查
select * from tb1;
select id,name from tb1;
select id,name from tb1 where id>12 or name=‘alex‘;
select id,name as cname from tb1 where id>12 or name=‘alex‘;
select name,age,11 from tb2 # 11是常量
select * from tb1 where id in (1,2,3);
select * from tb1 where id not in (1,2,3);
select * from tb1 where id between 1 and 5; # 闭区间
select * from tb1 where id in (select nid from 表); # 先执行括号语句
通配符:
select * from tb1 where name like "a%"; # 后续任意字符
select * from tb1 where name like "a_"; # 后续一个字符
分页:
select * from tb1 limit 10; # 前10项
select * from tb1 limit 0,10; #从0项开始前10项
select * from tb1 limit 10,10;
select * from tb1 limit 10 offset 20; # 从20开始前10项
排序:
select * from tb1 order by id asc;
select * from tb1 order by id desc;
取后10条数据
select * from tb1 order by id desc limit 10;
原文:https://www.cnblogs.com/farion/p/10043213.html