Mysql-DML(数据处理语言)
-- 插入数据
insert into member values(1,'张三',21,5000);
insert into member(id,name) values(2,'李四');
insert into member values(3,'王五',23,3000)
,(4,'纪六',24,4000)
,(5,'查七',25,5000);
-- 更新(修改数据) 不加限制条件 该字段全部修改
update member set age= 30 where id=1;
-- 删除数据
delete from member where name='王五';
-- 查询数据
-- 全查询
select * from member;
-- 部分查询 别名 as
select id as '卡号'
,name as '姓名'
from member;
-- 在字段中计算
select name ,salary+500 from member;
-- 条件查询
select * from member where salary = 4000;
-- 去重
select distinct * from member;
select * from member group by id;
-- 筛选条件 where
-- 比较运算符 > < >= <= != <>
select * from member where salary != 4000;
-- 逻辑运算符 and or not
select * from member where salary != 4000 and id > 3;
-- 在。。范围
-- in(a,b) not in() a和b
SELECT * FROM member where age in(21,24);
-- between a and b a到b
SELECT * FROM member where age BETWEEN 21 and 24;
-- 判断是否为空 is null/is not null
SELECT * FROM member where age is null;
-- 模糊查询 _匹配任意字符(占位符) %0或多个
SELECT * FROM member where name like '%六';
-- 分页 LIMIT(a)查看前a行,LIMIT(a,b)从查询的结果从第a个下标开始查看b行
-- m条,n页
-- SELECT * FROM student LIMIT m*(n-1),m;
-- 排序 ORDER BY 字段名 ASC -- 升序 DESC -- 降序
SELECT * FROM student ORDER BY score DESC,id desc;
-- 聚合函数
-- 统计次数 COUNT(*)
SELECT COUNT(*) FROM student ;
-- 求和 SUM
SELECT SUM(score) FROM student ;
-- 最大值 max
SELECT max(score) FROM student ;
-- 最小值 min
SELECT min(score) FROM student ;
-- 平均值 avg
SELECT avg(score) FROM student ;
-- 分组 group by 只能查询group by后的普通字段 和聚合函数
SELECT class,max(score) as max_score from student GROUP BY class HAVING max_score>=90;
-- where不能跟分组后面 ,having只能在分组里面
-- where后面不能有聚合函数,hiving可以
-- SELECT *,指定列,as 别名,聚合函数
-- from 表格名
-- where 逻辑运算符
-- group by 分组
-- having 分组后筛选
-- order by 排序(asc/desc)
-- 笛卡尔积:数量就是两个表数据相乘的结果
SELECT * FROM nation,tank;
-- 直接链接
SELECT * FROM a,b where a.id=b.id;
-- 内链接效果和直接链接一样 INNER JOIN
-- 左链接 以左表为主表 左全右不全 left JOIN
-- 右链接 RIGHT JOIN 以右表为主表 右全左不全 RIGHT JOIN
-- union 上下合并 union 去重 unoin all 不去重
-- 拼接字符串,CONCAT(name,'123')
select name,CONCAT(name,'123'),sex from a;
-- 四舍五入 round(1.732);
SELECT round(1.732);
-- 判断 if(判断条件,true语句,false语句)
SELECT id,name,sex,if(sex='男','m','w') FROM a;
-- 条件判断
-- case
-- when sm=30 THEN 'a'
-- when sm=bTHEN 'b'
-- when sm=50 THEN 'c'
-- else 'A'
-- end
-- 表转置
将表从横列 变纵段