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(增删改查补充)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 四叉树的应用_游戏算法(2)
- 下一篇: linux cmake编译源码,linu