要搭建网站,网站空间一般有多大,作文生成器,公司网站上荣誉墙怎么做一、 数据查询语言(DQL)(重中之重)完整语法格式#xff1a;select 表达式1|字段,....[from 表名 where 条件][group by 列名][having 条件][order by 列名 [asc|desc]][limit 位置,数量]1 普通查询select 查询表达式; // 最简单的sql语句#xff0c;是一个函数select…一、 数据查询语言(DQL)(重中之重)完整语法格式select 表达式1|字段,....[from 表名 where 条件][group by 列名][having 条件][order by 列名 [asc|desc]][limit 位置,数量]1 普通查询select 查询表达式; // 最简单的sql语句是一个函数select database();select version();select now();2 条件查询where 条件表达式, 支持运算符和函数MySQL支持的运算符、 !、 、 、 and、 or、 notis null、 is not nullbetween...and... (区间查询多少到多少之间)in(set);like 通配符和占位符: % _ (模糊查询)%: 表示0个或者多个字符_: 表示占位一个-- 查询所有的老师信息select * from teacher;-- 查询id 大于2的老师信息select * from teacher where id2;-- 查询姓名为空的老师信息 在数据库中null永远都不等于null那么怎么去判断null值通过 is null / is not null-- select * from teacher where namenull; # 错误select * from teacher where name is not null;-- 查询id为1 并且 姓名是 xiaosi的老师信息select * from teacher where id1 and namexiaosi;-- 查询id为1 并且 姓名是 xiaosi的老师信息select * from teacher where id1 or namexiaosi;-- 查询薪水在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 %\_%;3 分组查询语法格式[group by 列名] [haveing 条件]一般情况分组查询结合聚合函数一起使用max()min()sum()avg()count()-- 查询每个部门的平居薪资# 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;4 排序查询语法格式order by 列名 asc|desc 默认升序(asc)-- 查询老师信息根据薪资进行排序要求从大到小进行排序select * from teacher order by sal desc; # 根据sal进行降序排序select * from teacher order by sal asc; # 根据sal进行升序排序select * from teacher order by sal; # 根据sal进行升序排序, 利用默认排序5 限制结果集数量的查询(分页)编号 商品名称 商品价格 操作 1 玩具娃娃 100.0 删除 修改 2 玩具汽车 200.0 删除 修改 3 玩具飞机 300.0 删除 修改 ................................ 首页 上一页 1 2 3 4 5 下一页 尾页语法格式limit n条数;-------从第一条开始取n条数据(了解)limit start开始下标索引count条数; ---- 从起始位置start取count条数据(起始位置是从0开始的) (推荐使用)分页(每页显示两条数据)第一页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;分页公式:开始下标索引(起始位置) (当前页-1)*每页显示条数;-- 每页显示3条-- 显示第二页select * from teacher limit 3,3;6 扩展别名select * from teacher; # 查询表中所有字段记录select name, sal, dname from teacher; # 查询表中指定字段记录-- 给查询的字段设置别名 同时也可以给表设置别名 通过as 关键字实现别名select name as 姓名, sal 薪资, dname 部门名称 from teacher二、 事务控制语言(TCL)MySQL事务默认自动开启的在MySQL数据库中只有使用了Innodb数据库引擎的数据表或库才会支持事务通过事务来管理 insert、update、delete语句事务必须满足4个条件(ACID):原子性(不可分割性): 要么全部完成要么全部不完成不会结束在中间的某个环节。在执行的过程中一旦出现错误/异常会被回滚(Rollback)到事务开始前的状态就像这个事务从来没有执行过一样。一致性: 事务处理前后数据保持一致隔离性: 事务处理必须是独立的彼此隔离持久性: 事务对数据的修改永久保存1 为什么使用事务银行转账事务广泛使用订单系统银行系统等....2 MySQL事务控制commit(提交)rollback(回滚)savepoint(事务节点)3 实战操作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|true; // 设置事务的提交方式rollback; // 事务回滚commit; // 事务提交savepoint 节点名称; // 设置回滚的节点rollback to 节点名称; // 回滚到具体的某个节点set autocommit false; # 设置事务手动提交select * from student;delete from student where id1; # 删除id为1 的信息rollback; # 事务回滚commit; # 事务提交update student set money money-30000 where id1;savepoint t1; # 设置事务节点update student set money money-20000 where id1;rollback to t1; # 回滚到t1节点位置commit; # 事务提交