本文章为本人学习记录的笔记,便于之后反复查看,可能会有错,请谅解。看文先点赞,是一个美好的习惯。
本地登录mysql: mysql -uroot -p123456 退出mysql exit 一、单表查询 1. 查看mysql有那些数据库 show databases;? 选择使用数据库 use test;//使用一个叫test的数据库 创建一个数据库:create database fujianjian ;//创建一个叫fujianjian的数据库。 展示数据库下所有的表:show tables; ? 2.DQL select dname from dept; 3. select dname,deptno from dept; 4. select * ?from dept;//不建议使用,性能会很低 5. select deptno ,dname as deptname from dept; 6. select deptno,dname deptname from dept; 7. select deptno,dname 'dept name'from dept; (强调数据库中单引号才是最标准的) 8. select ename,sal*12 from emp; select ename sal*12 as yearsal from emp; ---条件查询? 9. select ename, empno from emp where sal=800; select ename, empno from emp where sal!=800; select ename ,empno from emp where sal<>800;(也是不等于) 10. select ename,empno,sal from emp where sal<2000; select ename,empno,sal from emp where sal<=2000; select ename,empno,sal from emp where sal>=2000; 11. select empno,enaem,sal from emp where sal >=2450 and sal<=3000; select empno,enaem,sal from emp where sal between 2450 and 3000; 12. select empno,ename,sal,comm from emp where comm is null; select empno,ename,sal,comm from emp where comm is not null; 13. select empno,ename,job,sal from emp where job='lisi' and sal>2500; 14. select empno,ename,ob from emp where job='haha' or job='niuniu'; 15. select empno,ename,ob from emp where sal>2500 and (deptno=10 or detno=20); 16. select empno,ename,ob from emp where job='haha' or job='niuniu'; select empno,ename,ob from emp where job in ('haha','niuniu'); select empno,ename,ob from emp where job not in ('haha','niuniu');//括号里面只代表具体值,不代表区间 17. like模糊查询%(任意多个字符)_(任意一个字符) select ename from emp where ename like '%O%';(中间包含O的)? select ename from emp where ename like '%T';(以T结尾) select ename from emp where ename like 'K%';(以K开始) select ename from emp where ename like '_A%';(第二个字母是A的) select ename from emp where ename like '__R%';(第三个字母是R的) select ename from emp where ename like '%\_%';(\转义,找出包含下划线的) 18. select ename,sal from emp order by sal;(默认就是升序) select ename,sal from emp order by sal desc;(desc降序) select ename,sal from emp order by sal asc;(asc 升序) 查询员工名字和薪资,要求按照薪资升序,当薪资一样的话,再按照名字升序排序; select ename,sal from emp order by sal asc,ename asc; //sal在前,起主导地方,只有前面相同,后面的排序才会执行 select ename,sal from emp order by 2;//2表示第二列。第二列是sal; ?? ??? ??? ? ? ? ? ? ? ?//不建议在开发中这样写,因为不健壮; 19. select ename,sal from emp where sal between 1250 and 3000 order by sal desc;(顺序不能变) ?20.单行数据处理函数: 20.1. select lower(ename) as ename from emp;//lower转换小写 select upper(ename) as ename from emp;//upper转换大写 20.2 select substr(ename,1,1)as ename from emp;(起始位置从1开始) //substr取子串(substr(被截取的字符串,起始下标,截取的长度)) 20.3? select length(ename)enamelength from emp;//length取长度 ? select concat(empon,ename) from emp; ? //concat函数进行字符串拼接 20.4? select ?* from emp where ename=trim(' ? KING'); //trim()去空格 20.5? select ? 'abc' from emp;生成14个abc ? //select后面跟一个字面值后,就会自动生成表结构的数量的值 ? ? ? ?select round(123.567,0) as result from emp;(0保留整数部分,1保留一位小数。-1保留到十位,个位用0补齐) //round生成一个随机数 20.6? ?select round(rand()*100,0) from emp; //生成 ?//rand()生成一个0到1的随机数 20.7? 有null值参与的数学运算结果均为null ,加法也不可以 select ename,(sal+ifnull(comm,0))*12 as yearsal from emp;//ifnull空值处理函数 ifnull(数据,被当做那个值) 20.8? case...when...then...when...then...else...end; 当员工的工作岗位是manager的时候,工资上调10%,当.....; select ename,job,sal from emp; select ename,job, (case job when 'manager' then sal*1.1 when 'salesman' then sal*1.5 else sal end) as newsal from emp; 21.分组函数,多行处理函数,输入多行,最终输出一行。 21.1? five ?count计数 ?sum求和 avg 平均值 max最大值 min最小值? select sum(salary) from emp;//先分组才可以使用聚合函数,在select后面默认整表为一组,在where后面不分组会报错 //count会自动忽略null,分组函数均会忽略null,这五个函数均会忽略null 21.2 count 某个具体字段和count(*) 的区别;count(*)计算全表的行数;count 某个字段会自动忽略null; 21.3 分组函数不能直接使用在where 子句中因为执行顺序为(from>where>group by >select>order by)因为where的时候还没分组 22.分组查询(非常重要;五星*****)select ... from... where..group by...order by...执行顺序如上解释 22.1 找出每个岗位的工资和//按照工作岗位分组,然后对工资求和 select job,sum(sal)from emp group by job;注意查询字段只能添加分组关键词和分组函数,其他一律不能跟; select ?deptno,max(sal) from emp group by deptno; 22.2 找出每个部门,不同工作岗位的最高薪资;//技巧:两个字段联合成1个字段看。(两个字段联合分组) select job,deptno,max(sal) from emp group by deptno,job; 22.3? 使用having可以进行过滤,必须跟group by联合使用 找出每个部门最高薪资,要求显示最高薪资大于3000的? select deptno,max(sal) from emp group by deptno having max(sal)>3000; //能用where过滤掉的数据,先用where过滤,效率会比having高,如果where解决不了的在用having子句 select deptno,max(sal)from emp where sal>3000 group by deptno; where没办法的?找出每个部门的平均薪资,要求显示薪资高于2500的; select deptno,avg(sal)from emp group by deptno having avg(sal)>2500; 单表查询的大总结: select ... from...where...group by...order by... 执行顺序1.from 2.where 3.group by 4.having 5.select 6.order by 找出每个岗位的平均薪资,要求显示平均薪资大于1500的,除了manger岗位之外的,要求按照平均薪资降序排列 select job,avg(sal) as avgsal from emp where job <> 'manger' group by job having avg(sal)>1500 order by avgsal desc; ---------------------------------------------------------------------------------------------------------------------------------
|