4. 表内容操作

4.1. 增

1
2
3
4
5
6
insert into  (列名,列名...) values (,,...)
insert into  (列名,列名...) values (,,...),(,,...)
insert into  (列名,列名...) select (列名,列名...) from 

例:
insert into tab1(name,email) values('zhangyanlin','zhangyanlin8851@163.com')

4.2. 删

1
2
delete from                                       # 删除表里全部数据
delete from  where id1 and name'zhangyanlin'   # 删除ID =1 name='zhangyanlin' 那一行数据

4.3. 改

1
update  set name  'zhangyanlin' where id>1

4.4. 查

1
2
3
select * from 
select * from  where id > 1
select nid,name,gender as gg from  where id > 1

4.5. 条件判断

4.5.1. where

1
2
3
4
5
select * from <table> where id >1 and name!='huwh' and num =12;
select * from <table> where id between 5 and 6;
select * from <table> where id in (11,22,33);
select * from <table> where id not in (11,22,33);
select * from <table> where id in (select nid from <table>)

4.5.2. 通配符like

1
2
select * from <table> where name like 'hu%';   #hu开头
select * from <table> where name like 'hu_'    #hu开头后接一个字符

4.5.3. 限制limit

1
2
3
select * from <table> limit 5;   #5
select * from <table> limit 4,5  #从第四行开始的5
select * from <table> limit 5 offset 4;#从第四行开始的5

4.5.4. 排序asc,desc

1
2
3
select * from <table> order by  asc;            #跟据“列”从小到大排序(不指定默认为从小到大排序)
select * from <table> order by  desc;           #根据“列”从大到小排序
select * from <table> order by 1 desc,2 asc;  #根据“列1”从大到小排序,如果相同则按“列2”从小到大排序

4.5.5. 分组group by

group by 必须在where之后,order by之前。

1
2
3
4
5
6
select num,from <table> group by num;     
select num,nid from <table> group by num,nid;
select num from <table> where nid > 10 group by num,nid order nid desc;
select num,nid,count(*),sum(score),max(score) from <table> group by num;
select num from <table> group by num having max(id) > 10;
select num from <table> group by num;

like 通配符知识点

%百分号通配符: 表示任何字符出现任意次数 (可以是0次). _下划线通配符:表示只能匹配单个字符,不能多也不能少,就是一个字符.

mysql事务

开始事务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
begin 
start transaction

# 提交
commit
commit  work

#回滚
rollback
rollback work