– https://www.runoob.com/mysql/mysql-functions.html 手册
基本查询
select 列名 as 别名 from 表
- 别名如果有特殊字符要用‘’引起来
条件查询
select 列名 as 别名 from 表 where 条件
条件运算符: < , > , = , != , >=, <=, <>(不等),<=>(严格比较两个值是否都为null,一个不是就返回0,都是返回1)
逻辑运算符: and, or ,not ,xor(异否,如果都成立或者失败返回0,一个成功一个失败返回1)
模糊查询:like(% 模糊匹配过个,_模糊匹配一个)
between and 包含两端
in ()
is null/is not null
排序查询
order by 排序字段 asc/desc (升序,降序)
一般在sql最后(limit之前)
可以按 单字段,多字段,函数,表达式,别名来排序
多表查询
如果两个表直接联表查询不加where,查询结果是笛卡尔积(交叉连接)
笛卡尔积:例子 (1,2), (3,4)
上两个集合的笛卡尔积是({1,3},{1,4}, {2,3},{2,4})
内连接
select * from table1 a inner join table2 b on a.列 = b.列 where 条件列式
根据on的条件连接,只有在两个表中都有对应的行才会有显示出来
例子: table1({a:1, b:2}, {a:23, b:21}) table2({c:12, d:21 }, { c:1,d:21})
select * from table1 a inner join table2 b on a.b = b.b
结果:
a b c d
23 21 12 21
23 21 1 21
外连接(左,右,全连接) out 可以省略
左连接
select * from table1 as a left (out) jion table2 as b on a.列 = b.列 where 条件列式
将table1全部展示,根据on的条件连接,table2如果有对应的行则展示,否则为null
- 注意如果 1表对应2表是1对多,那么1表1行会有多条记录,如果是多对一,记录数不会变,只是b表数据都是同一行
例子1: table1({a:1, b:2}, {a:23, b:21}) table2({c:12, d:21 }, { c:1,d:21})
select * from table1 a left join table2 b on a.b = b.b
结果 :
a b c d
1 2 null null
23 21 12 21
23 21 1 21
- 将1,2表换一下 例1和例2对应我注重的特殊情况
例子2: table1({a:1, b:2}, {a:23, b:21}) table2({c:12, d:21 }, { c:1,d:21})
select * from table2 a left join table1 b on a.b = b.b
结果 :
a b c d
23 21 12 21
23 21 1 21
右连接
select * from table1 as a right (out) jion table2 as b on a.列 = b.列 where 条件列式
将table2全部展示,根据on的条件连接,table1如果有对应的行则展示,否则为null
- 注意如果 2表对应1表是1对多,那么2表1行会有多条记录,如果是多对一,记录数不会变,只是a表数据都是同一行
这个其实就是左连接换个方向而已,即以table2为主,table1为附属表,也会出现table2跟table1有1对多
那么就会加记录数 而 多对1则会table1会同一行多匹配table2
- 只写一个例子看看吧
例子1: table1({a:1, b:2}, {a:23, b:21}) table2({c:12, d:21 }, { c:1,d:21})
select * from table2 a right join table1 b on a.b = b.b
= select * from table1 a left join table2 b on a.b = b.b
结果 :
a b c d
1 2 null null
23 21 12 21
23 21 1 21
全连接
就是将左右连接用union连接起来
union和union all 区别 :union没有重复, union all 有重复
自连接
利用别名机制,自己和自己进行连接查询(必须有别名)
select * from table1 as a, table2 as b where 条件列式
例子1: table1({a:1, b:2}, {a:23, b:21}) table2({c:12, d:21 }, { c:1,d:21})
select * from table1 as t1, table2 as t2 where t1.a = 1 and t1.b < t2.d
结果 :
a b c d
1 2 12 21
1 2 1 21
子查询
一个查询语句内部嵌套了查询语句,这个查询语句就叫子查询,外面的叫主查询
- 子查询先执行,然后执行外查询
子查询分类: 多列单行子查询, 单列单行子查询, 多列多行子查询, 多列多行子查询
如果是单行可用逻辑运算符: <,>,<>,=,!=,>+,<=
多行可用逻辑运算符: any,some(any和some是只要条件有一个符合就为true),all,not in, in
- 多行逻辑运算符一般都是和单行一起使用在多行子查询中
根据子查询位置,可以分为
参数内
- 作为参数要注意当你主查询有多少记录的时候,你的子查询就会有多少次
作为表
- 作为表 要注意一定要有别名
条件列式
条件列式主要是注意多列
例子:
select * from table1 where (table1.a列, table2.b列) in some (select table2.a列,table2.b列 from table2);
这里table1的a列和table2的a列对应, table1的b列和 table2的b列对应
分组查询
select 分组函数,分组列,表达式
from 数据表
where 分组前数据筛选条件式
group by 分组列
having 分组后数据筛选条件式
order by (asc/desc) 分组后排序字段,表达式等
limit 分组后数量限制
– 分组函数
sum() 求和
avg() 平均
max() 最大
min() 最小
count(*) 统计
sum和avg只是是数值类型,其他什么类型都可以
所有分组函数都会忽略null
count()和count(1)都是统计结果集的数量,count()的比较快
查询里面必须有分组列
单行函数
https://www.runoob.com/mysql/mysql-functions.html
主要分为 字符串函数, 数学函数,日期函数还有高级函数
具体太多,concat(),length(),char_length(),sum(),max()
ceil() – 向上取整
floor() --向下取整
round(x,y) --四舍五入
运算符优先级(14为优先级最高 1 为最低)
一般是位取反,位异或,加减乘除,位运算符,比较运算符,逻辑运算符 这个顺序