- 增
- insert into t1(name) values (‘小明‘),(‘小红‘) ; # 一次性其实可以插入多条的
- insert into t1(name) select name from t2; # 可以从别的表复制一份出来
- 删
- delete from t1 where xx =!>< and or # 后可以接where条件语句,支持逻辑符号 != 也可以写成 <>
- 改
- update t1 set name = ‘xx‘ where # 同理可以接where
- update t1 set name = ‘xx‘ ,age = 12 # 可以改多列
- 查*
- select id from t2 where .... # 同理
- select id as idd from t2 ; # 可以修改查看时的表头(列的标题名称),且不影响原来的内容
- select id,11 from t2; # 可以在查询的地方加上一个常量,这样查看的时候表头会多出一列标题和内容的是常量的列
- select * from t1 where id in/not in (1,3,5); # 可以查询id是/不是1,3,5的,不需要用多个or
- select * from t1 where id between 1 and 3; # 闭区间,取1,2不取3
- select * from t1 where id in(select id from id2) # 可以查交集
- 通配符:select * from t1 where name like ‘a%‘
- 以a开头
- a% 表示ab,abc,abcd..... 范围比较广,可以匹配多位
- a_ 表示ab,ac,ad 只能匹配一位
- 以a结尾 %a,_a
- 包含a %a%,%a_ ......
- select * from t1 limit 10; # 取前10个
- select * from t1 limit 20,10 # 从20开始,向后取10条
- select * from t1 limit 10 offset 20 # 效果和上面??一样
- 排序:
- select * from t1 order by id desc; # 从大到小
- select * from t1 order by id asc; # 从小到大
- select * from t1 order by id desc limit 10 # 这样就可以取后十条了
Python MySQL(SQL语句的补充)
原文:https://www.cnblogs.com/otome/p/12455595.html