完整语法格式:sql
MySQL支持的运算符:数据库
like 通配符和占位符: % _ (模糊查询)函数
-- 查询全部的老师信息 select * from teacher; -- 查询id 大于2的老师信息 select * from teacher where id>2; -- 查询姓名为空的老师信息 在数据库中null永远都不等于null,那么怎么去判断null值?经过 is null / is not null -- select * from teacher where name=null; # 错误 select * from teacher where name is not null; -- 查询id为1 而且 姓名是 "xiaosi"的老师信息 select * from teacher where id=1 and name='xiaosi'; -- 查询id为1 而且 姓名是 "xiaosi"的老师信息 select * from teacher where id=1 or name='xiaosi'; -- 查询薪水在2000到10000之间的老师信息 select * from teacher where sal >=2000 and sal <=10000; select * from teacher where sal between 2000 and 10000; # 这种方式等同于上面 -- 查询姓名中有 ‘尘’ 字的老师信息 select * from teacher where name like '%尘%'; -- 查询姓名是三个字的 select * from teacher where name like '___'; -- 查询姓 '小' 的老师信息 select * from teacher where name like '小%'; -- 查询名字中含有下划线的老师信息 '\' 转义 -- select * from teacher where name like '%_%'; # 错误 select * from teacher where name like '%\_%';
语法格式:code
通常状况分组查询结合聚合函数一块儿使用排序
-- 查询每一个部门的平居薪资 # select * from teacher GROUP BY dname # 记住:分组的正确使用方式,group by 后面没有出现的列名不能出如今select 和from 的中间, # 虽然不报错,可是不是分组的正确使用方式 # 聚合函数中出现的列名group by后面没有无所谓 select dname from teacher GROUP BY dname; select dname, avg(sal) from teacher GROUP BY dname;
语法格式:索引
-- 查询老师信息,根据薪资进行排序,要求从大到小进行排序 select * from teacher order by sal desc; # 根据sal进行降序排序 select * from teacher order by sal asc; # 根据sal进行升序排序 select * from teacher order by sal; # 根据sal进行升序排序, 利用默认排序
编号 商品名称 商品价格 操做
1 玩具娃娃 100.0 删除 修改
2 玩具汽车 200.0 删除 修改
3 玩具飞机 300.0 删除 修改
................................
首页 上一页 1 2 3 4 5 下一页 尾页事务
语法格式it
分页(每页显示两条数据) 第一页:select * from teacher limit 0,2; 第二页:select * from teacher limit 2,2; 第三页:select * from teacher limit 4,2; 第四页:select * from teacher limit 6,2; 第五页:select * from teacher limit 8,2;
分页公式:io
-- 每页显示3条 -- 显示第二页 select * from teacher limit 3,3;
select * from teacher; # 查询表中全部字段记录 select name, sal, dname from teacher; # 查询表中指定字段记录 -- 给查询的字段设置别名 同时也能够给表设置别名 经过as 关键字实现别名 select name as '姓名', sal '薪资', dname '部门名称' from teacher
MySQL事务默认自动开启的
事务必须知足4个条件(ACID):table
create table student( id int, name varchar(32), age int, money double ); insert into student values(1, '老王', 18, 60000); select * from student; rollback;
手动关闭事务提交语法:
set autocommit = false; # 设置事务手动提交 select * from student; delete from student where id=1; # 删除id为1 的信息 rollback; # 事务回滚 commit; # 事务提交 update student set money = money-30000 where id=1; savepoint t1; # 设置事务节点 update student set money = money-20000 where id=1; rollback to t1; # 回滚到t1节点位置 commit; # 事务提交