赞
踩
视频链接:康师傅mysql
1.select查询语句没有指定排序默认查询出来是添加的顺序
2.升序(ASC) ascend,降序(DESC) descend,如果order后面没有指明用什么排序,默认使用的是升序的方式ASC
3.在where后面不能使用列的别名(和sql语句的执行顺序有关系)
4.二级排序,一级排好之后,一级相同的再按照二级进行排序
5.mysql8新特性:SELECT * FROM aa limit 2 offset 0;这个offset和之前limit后面两个值的含义相反
6.笛卡尔积的概念:笛卡尔乘积是一个数学运算.假设我有两个集合X和Y,那么X和Y的笛卡尔积就是X和Y的所有可能组合,也就是第一个对象来自于X,第二个对象来自于Y的所有可能.组合的个数即为两个集合中元素个数的乘积数.
7.笛卡尔积的错误会在下面条件下产生:
8.为了避免笛卡尔积,可以在where加连接条件
9.如果查询语句中出现多个表都存在的字段,则必须指明字段所在的表
10.从sql优化的角度看,建议每个字段都加表名
11.多表查询的分类:
外连接的分类:
解决满外连接的方法:
12.7种sql joins
中间图:内连接
SELECT employee.id,employee.`name`,department.`name` from department,employee where employee.did=department.id;
左上图:左外连接
SELECT employee.id,employee.`name`,department.`name` from employee left join department on employee.did=department.id;
右上图:右外连接
SELECT employee.id,employee.`name`,department.`name` from employee right join department on employee.did=department.id;
左中图:左上图取左边
SELECT employee.id,employee.`name`,department.`name` from employee left join department on employee.did=department.id where department.name is null;
右中图:右上图取右边
SELECT employee.id,employee.`name`,department.`name` from employee right join department on employee.did=department.id where employee.name is null;
左下图(也就是满连接)
第一种方法:左上图和右中图结合
SELECT employee.id,employee.`name`,department.`name` from employee left join department on employee.did=department.id union all SELECT employee.id,employee.`name`,department.`name` from employee right join department on employee.did=department.id where employee.name is null;第二种方法:右上图和左中图结合
SELECT employee.id,employee.`name`,department.`name` from employee right join department on employee.did=department.id union all SELECT employee.id,employee.`name`,department.`name` from employee left join department on employee.did=department.id where department.name is null;第三种方法:右下图和中间图结合
SELECT employee.id,employee.`name`,department.`name` from employee left join department on employee.did=department.id where department.name is null union all SELECT employee.id,employee.`name`,department.`name` from employee right join department on employee.did=department.id where employee.name is null union all SELECT employee.id,employee.`name`,department.`name` from department,employee where employee.did=department.id;
右下图:左中图和右中图结合
SELECT employee.id,employee.`name`,department.`name` from employee left join department on employee.did=department.id where department.name is null union all SELECT employee.id,employee.`name`,department.`name` from employee right join department on employee.did=department.id where employee.name is null;
13.自然连接:sql99中,用natural join用来表示自然连接,它会帮你自动查询两张连接表中,所有相同的字段,然后进行等值连接
14.using连接:使用using指定数据表中的同名字段进行等值连接,但是只能配合json一起使用
15.查看表结构:desc
16.函数:
数值函数
函数 | 用法 |
ABS(x) | 返回x的绝对值 |
SING(x) | 返回x的符号,正数返回1,负数返回-1,0返回0 |
PI() | 返回圆周率的值 |
CEIL(x),CEILING(x) | 返回大于或等于某个值的最小整数 |
FLOOR(x) | 返回小于或等于某个值的最大整数 |
LEAST(e1,e2,e3...) | 返回列表中的最小值 |
GREATEST(e1,e2,e3...) | 返回列表中的最大值 |
MOD(x,y) | 返回x除以y以后的余数 |
RAND(x) | 返回0-1的随机值,其中x的值用作种子值,相同的x值会产生相同的随机数 |
RAND | 返回0-1的随机值 |
ROUND(x) | 返回一个对x的值进行四舍五入后,最接近于x的整数 |
ROUND(x,y) | 返回一个对x的值进行四色五入后最接近x的值,并保留到小数点后面y位 |
TRUNCATE(x,y) | 返回数字x截断为y位小数的结果 |
SQRT(x) | 返回x的平方根.当x的值为负数时,返回null |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。