一、增
insert into student(name, age) values('tom', 20); insert into student(name, age) values('tom', 18),('joker', 18); insert into student(name, age) select name, age from info;
二、删
delete from db1; delete from db1 where 条件判断 delete from db1 where 条件判断 and 条件判断 delete from db1 where 条件判断 or 条件判断 条件判断 = != < <= > >= truncate table db1;
三、改
update db1 set name = 'master'; update db1 set name = 'joker' where age = 20;
四、查
前面 select * from db1; select name,age from db1; select name as n, age from db1; 别名 select name,1 from db1; 条件 select * from tb1 where name = 'tom'; # 条件判断 select * from tb1 where name = 'tom' and age >1; select * from tb1 where name = 'tom' or name = 'joker'; select * from tb1 where name in ('tom', 'joker') select * from tb1 where name not in ('tom', 'joker'); select * from tb1 where id between 1 and 5; # 闭区间 select * from tb1 where id in(select id from info); 通配符 select * from tb1 where name like 'a%'; select * from tb1 where name like 'a_'; 注意:% 匹配任意字符,_ 匹配一个字符 分页 select * from tb1 limit 位置, 长度; # 若limit后面,只有一个参数,则是长度 select * from tb1 limit 长度 offset 位置; 排序 select * from tb1 order by id asc; select * from tb1 order by id desc; 分组 select count(cid), caption from class group by caption; select count(cid), caption from class where id > 5 group by caption having count(cid) >1; 聚合函数: max(列) min(列) sum(列) avg(列) count(列) 计算 注意:对聚合函数的结果进行比较要使用 having 相当于 where, where 在 最前面 连表操作 select * from userinfo1,department where userinfo.id=department.d_id; # 不常用 select * from userinfo left join department on userinfo.id=department.d_id; # 左边全显示,右边没有补NULL select * from userinfo inner join department on userinfo.id = department.d_id; # 不显示NULL select * from userinfo right join department on userinfo.id = department.d_id; # 右边的全显示,左边没有补NULL 注意:left join, right join, inner join, 都用on ',' 用where 前面的列,若是表有相同的列 表名.列名 来表示
补充
1.distinct 有去重的效果,但最好不要使用,效率低
select distinct username from userinfo; 2.查询多个表,会出现笛卡尔积现象
3.映射可以是查询语句
select (select name from info) from userinfo;
4.条件语句
case when 条件 then 为True else 为false end
6.判断是否为空
if(isnul(数据), 0, 1)
7.unoin
上下链表,注意列要一样
具有去重的作用,union all不去重
select * from userinfo
union
select * from info;