执行顺序:
from → join on → where
→ group by → with rollup | cube → having
→ select → distinct
→ order by
→ top | offset | fetch
union select*from `t1` where condition union [all | distinct] select*from `t2` where condition;
INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
where 的子句
like select*from `t` where `field` like `condition`;
select*from `t` where `grade` like `9%`;
IS NULL: 当列的值是 NULL,此运算符返回 true。
IS NOT NULL: 当列的值不为 NULL, 运算符返回 true。
<=>: 比较操作符(不同于=运算符),当比较的的两个值为 NULL 时返回 true。
ifnull(expr1,expr2) 如果expr1不为空,则值为expr1,如果为空则值为expr2
cast(field as datatype) cast(avg(score) as decimal(18,0)) 将一个符串转为decimal型
case `field` when condition then A else null end
group by select*, count(*) from `t` where condition group by `field`;
[count()|sum()|avg()]
order by select*from `t` where condition order by `field_a` desc,`field_b` asc ; #(default=asc)
with rollup 统计
select coalesce(a,b,c);
如果a==null,则选择b;如果b==null,则选择c;如果a!=null,则选择a;如果a b c 都为null ,则返回为null(没意义)
SELECT coalesce(name, '总数'),name=null 则,为'总数',,和 with rollup 配合用
left(field,1)从左边第一个开始,截取1个长度
right(field ,3)从右边开始截取3个长度
substring(str,pos) 从第几个开始截取, select substring('www.baidu.com',9) ;输出 u.com
substring(str,pos,length) select substring('www.baidu.com',9,3) ;输出 u.c
substring(str,pos,length) select substring('www.baidu.com',-9,3) ;输出 bai
substring_index(str,delim,count)字符串,关键字,关键字出现的次数
select substring_index('www.baidu.com','.',2) 第二次'.'之前的所有字符 输出 www.baidu
select substring_index('www.baidu.com','.',-2) 倒数第二次'.'之后的所有字符 输出 baidu.com
select substring_index('www.baidu.com','xixi',1) 若关键字不存在,返回整个字符串
事务:
transaction begin ...rollback(全部撤销)...commit(提交修改)
from → join on → where
→ group by → with rollup | cube → having
→ select → distinct
→ order by
→ top | offset | fetch
union select*from `t1` where condition union [all | distinct] select*from `t2` where condition;
INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
where 的子句
like select*from `t` where `field` like `condition`;
select*from `t` where `grade` like `9%`;
IS NULL: 当列的值是 NULL,此运算符返回 true。
IS NOT NULL: 当列的值不为 NULL, 运算符返回 true。
<=>: 比较操作符(不同于=运算符),当比较的的两个值为 NULL 时返回 true。
ifnull(expr1,expr2) 如果expr1不为空,则值为expr1,如果为空则值为expr2
cast(field as datatype) cast(avg(score) as decimal(18,0)) 将一个符串转为decimal型
case `field` when condition then A else null end
group by select*, count(*) from `t` where condition group by `field`;
[count()|sum()|avg()]
order by select*from `t` where condition order by `field_a` desc,`field_b` asc ; #(default=asc)
with rollup 统计
select coalesce(a,b,c);
如果a==null,则选择b;如果b==null,则选择c;如果a!=null,则选择a;如果a b c 都为null ,则返回为null(没意义)
SELECT coalesce(name, '总数'),name=null 则,为'总数',,和 with rollup 配合用
left(field,1)从左边第一个开始,截取1个长度
right(field ,3)从右边开始截取3个长度
substring(str,pos) 从第几个开始截取, select substring('www.baidu.com',9) ;输出 u.com
substring(str,pos,length) select substring('www.baidu.com',9,3) ;输出 u.c
substring(str,pos,length) select substring('www.baidu.com',-9,3) ;输出 bai
substring_index(str,delim,count)字符串,关键字,关键字出现的次数
select substring_index('www.baidu.com','.',2) 第二次'.'之前的所有字符 输出 www.baidu
select substring_index('www.baidu.com','.',-2) 倒数第二次'.'之后的所有字符 输出 baidu.com
select substring_index('www.baidu.com','xixi',1) 若关键字不存在,返回整个字符串
事务:
transaction begin ...rollback(全部撤销)...commit(提交修改)