dml语句
select基本语法
select * from tablename;*指代所有列名的快捷方式
select 列名1,列名2...列名n from tablename;
select 选择列名 from tbname where 表达式;
简单查询,基本格式:
select 列名1,列名2....
from 表1,表2.....
where 条件表达式
group by 分组
having 分组条件
order by 列名
limit num;
选择列
select distinct dept_id from s_emp;
+ - * / mod() abs() round()等
2.字符串相关联函数
concat,lower,upper
select concat(first_name,' ',last_name) from s_emp;
3.重命名,起别名
格式:select 列名1 as 别名1,列名2 as 别名2 from 表:
select id as d_id from s_emp;
4.对null的显示处理
ifnull(列名,指定显示值)
5.时间函数
date_format(curdate(),%y %m %d %h:%i:%s);
按照指定格式输出时间信息:
subdate(curdate(),days) 往前数days
adddate(curdate(),30) 往后退30天
subtime(curtime(),10) 往前推10秒
addtime(curtime(),10) 往后退10秒
where 条件
表达式组成:(列名,操作符,数值) and|or|&&|||(列名 操作符 数值)
!=,<> 不等于
= 判断是否等于
<=> 判断是否相等:针对于null
2.逻辑操作符:
例如:找部门编号41,工资大于1000元或者部门编号42,工资小于2000元的员工基本信息;
select id,dept_id,salary from s_emp where (dept_id=41 and salary>1000) or (dept_id=42 && salary<2000);
group by 分组显示
组函数:使用方法:写在having后面
having 子句
用于过滤和筛选;分组前使用where进行过滤,分组后使用having进行过滤。
order by排序
asc升序(默认) desc 降序
使用方法:sql语句最后+order by 列名
也可以多字段排序:order by 列名[asc] ; 列名[desc]
limit
限制行数,条数
limit 数字值
子查询
一个select语句的结果,作为另一个select语句的条件:
子查询:查询结果是主查询的对象(from)
多表查询
使用连接的方式,进行多表查询
等值连接
外链接
等值连接(内连接),inner join 获取交集外链接分为三种:
左外连接
右外连接
全连接
如对本文有疑问, 点击进行留言回复!!
一步步教你用Prometheus搭建实时监控系统系列(一)——上帝之火,普罗米修斯的崛起
网友评论