MySQL学习小结(一)常用的查询语句

版权声明: https://blog.csdn.net/qq_21578125/article/details/89082369
-- 基本数据库的命令
select database();-- 查看当前使用的数据库
select version();-- 查询数据库的版本
show databases;-- 查询现有的数据库
show tables;-- 查看当前库中的表
show tables from mysql;-- 查询其他库中的表
desc dept;-- 查看表结构
show create table emp;-- 查看表的创建语句
select * from emp;-- 查询emp的全部字段
select empno,ename,sal*12 from emp;-- 列出员工的年薪
select empno as `员工编号`,ename as `员工姓名`,sal*12 as `年薪` from emp;-- 将查询出的字段显示为中文

-- 条件查询
select empno, ename, sal from emp where sal <> 5000;-- <>操作符
select empno, ename, sal from emp where sal>=1600 and sal<=3000;
select empno, ename, sal from emp where sal between 1600 and 3000;-- between...AND...
select * from emp where comm=null;-- is null 
select * from emp where job='MANAGER' and sal > 2500;-- and
select * from emp where job='MANAGER' or job='SALESMAN';-- or
select * from emp where sal > 1800 and (deptno = 20 or deptno = 30);-- 表达式优先级尽量使用括号
select * from emp where job in ('manager','salesman');-- in
select * from emp where not (sal = 1600 or sal = 3000); -- not
select * from emp where sal <> 1600 and sal <> 3000;-- not第二种写法
select * from emp where sal not in (1600, 3000); -- not 第三种写法
select * from emp where ename like 'M%';-- 	Like可以实现模糊查询,like支持%和下划线匹配,%匹配任意字符出现的个数,下划线只匹配一个字符
-- Like 中的表达式必须放到单引号中|双引号中,

-- 排序数据

-- 排序采用order by子句,order by后面跟上排序字段,排序字段可以放多个,多个采用逗号间隔,order by默认采用升序,如果存在where子句那么order by必须放到where语句的后面

select * from emp order by sal desc;
select * from emp where job='MANAGER' order by sal;
select * from emp order by job,sal;
select * from emp order by sal asc;-- 从小到大
select * from emp order by sal desc;-- 从大到小
select * from emp order by job desc, sal desc;
select * from emp order by 6;-- 按照第六个字段排序

-- 数据处理函数/单行处理函数

select lower(ename) from emp;
select * from emp where job = upper('manager')
select * from emp where substr(ename,1,1)=upper('m');-- 取子串(substr(被截取的字符串,起始下标,截取的长度)) 
select length(ename),ename from emp where length(ename)=5;
select * from emp where job=trim(upper('manager  '));-- trim 会去除首位空格,但是中间的不会去除
select * from emp where HIREDATE=str_to_date('1981-02-20','%Y-%m-%d');-- 将字符串转换成日期
select empno, ename, date_format(hiredate, '%Y-%m-%d %H:%i:%s') as hiredate from emp;-- 将入职日期格式化成yyyy-mm-dd hh:mm:ss
select empno,ename,format(sal,0) from emp;-- 查询薪水加入千分位数
select empno,ename,format(sal,2) from emp;-- 查询薪水保留两位小数
select round(123.56)-- 四舍五入
select rand();-- 生成随机数
select * from emp order by  rand() limit 2;-- 随机抽取记录数,order by 必须写上

-- case … when … then …..else …end
select empno,ename,job,sal,case job when 'MANAGER' then sal*1.1 when 'SALESMAN' then sal*1.5 end as newsal from emp;-- 如果job为MANAGERG薪水上涨10%,如果job为SALESMAN工资上涨50%,其他的工资为空
select e.*,sal ,case job when 'salesman' then sal*1.1 when 'clerk' then sal*1.2  else sal end as new_sal from emp e;-- 其他的工资不动,需要添加else
select ifnull(comm ,0) from emp;-- 如果comm 为空,替换为0

-- 分组函数/聚合函数/多行处理函数

/*
count 取得计数
sum 求和
avg 去平均
max 取最大的数
min 取最小的数

分组函数自动忽略空值,不需要手动的加where 条件排除空值
分组函数不能直接使用在where 关键字的后面
*/
select count(*) from emp;
select count(comm) from emp;-- 采用count(字段名称),不会取得为null的记录
select sum(sal) from emp;-- 可以取得某一个列的和,null会被忽略
select sum(sal+comm) from emp; -- 只计算了sal和comm都非空的值
select sum(sal +ifnull(comm,0))  from emp;-- 把null 替换为0,排除null值
select avg(sal) from emp;
select max(sal) from emp;
select max(str_to_date(hiredate,"%Y-%m-%d")) from emp;
select min(sal) from emp;
select count(*),sum(sal),avg(sal),max(sal),min(sal) from emp;

-- 分组查询 group by 和having

select job,sum(sal) from emp group by job; 
select job,sum(sal) from emp group by job order by sum(sal) ;-- order by 放到group by 的后面
/*在SQL语句中若有group by 语句,那么在select语句后面只能跟分组函数+参与分组的字段。*/
select job,avg(sal) from emp group by job having avg(sal)>2000;
/*
分组函数的执行顺序:
根据条件查询数据
分组
采用having过滤,取得正确的数据
*/

/*
select 语句总结:
一个完整的select 语句的格式:
select 字段 from 表名 where ……. group by …….. having …….(就是为了过滤分组后的数据而存在的—不可以单独的出现) order by ……..
执行顺序:
1.	首先执行where语句过滤原始数据
2.	执行group by进行分组
3.	执行having对分组数据进行操作
4.	执行select选出数据
5.	执行order by排序
原则:能在where中过滤的数据,尽量在where中过滤,效率较高。having的过滤是专门对分组之后的数据进行过滤的。
*/

-- 连接查询
select e.ename, e.sal, d.dname from emp e inner join dept d on e.deptno=d.deptno where e.sal>2000;-- 内连接

-- 外连接
select e.ename, e.sal, d.dname from emp e right join dept d on e.deptno=d.deptno;
select e.ename, e.sal, d.dname from emp e left join dept d on e.deptno=d.deptno;
/*
连接分类:
内链接
* 表1  inner join  表2  on  关联条件
*  做连接查询的时候一定要写上关联条件
*  inner 可以省略
外连接
*左外连接
* 表1  left  outer  join  表2  on  关联条件
*  做连接查询的时候一定要写上关联条件
*  outer  可以省略*右外连接
* 表1  right  outer  join  表2  on  关联条件
*  做连接查询的时候一定要写上关联条件
*  outer  可以省略
*左外连接(左连接)和右外连接(右连接)的区别:
*左连接以左面的表为准和右边的表比较,和左表相等的不相等都会显示出来,右表符合条件的显示,不符合条件的不显示
*右连接恰恰相反,以上左连接和右连接也可以加入outer关键字,但一般不建议这种写法,
*/

-- 子查询

--  在where 中使用子查询,也就是在where语句中插入select 语句

-- 1. 查询员工信息,查询哪些人是管理者,要求显示出员工编号和员工姓名
select empno,ename from emp where empno in (select mgr from emp where mgr is not null);  
-- 2. 查询哪些人的薪水高于员工的平均薪水,需要显示员工编号,员工姓名,薪水
select empno,ename,sal from emp where sal >(select avg(sal)from emp);

-- 在from字句中使用子查询,可以将该子查询看做一张表

-- 1. 查询员工信息,查询哪些人是管理者,要求显示出其员工编号和员工姓名
select e.empno,e.ename from emp as e join (select distinct mgr from emp where mgr is not null )as m on e.empno=m.mgr; 
-- 2.查询各个部门的平均薪水所属等级,需要显示部门编号,平均薪水,等级编号
select deptno,avg(sal) as avg_sal from emp group by deptno;
select * from salgrade;
-- ->
select a.deptno,a.avg_sal,g.grade from (select deptno ,avg(sal) as avg_sal from emp group by deptno) as a join salgrade as g on a.avg_sal between g.losal and hisal;

-- 在select语句中使用子查询
select e.ename,d.dname from emp as e ,dept as d where e.deptno=d.deptno;
select e.ename,(select d.dname from dept as d where d.deptno=e.deptno) as dname from emp as e;


-- union
select * from emp where job='MANAGER' union select * from emp where job='SALESMAN'

-- limit的使用
select * from emp limit 5;-- 去前五条数据
select * from emp limit 1,5;-- 从第二条数据开始取5条




猜你喜欢

转载自blog.csdn.net/qq_21578125/article/details/89082369