select id,name from emp where id > 3 and id < 6;
from # 确定到底是哪张表
where # 根据过滤条件,筛选数据
select # 拿出筛选出来的数据中的某些字段
当表字段特别多的时候,结果的排版可能会出现混乱的现象,你可以在查询语句加\G来规范查询结果
select * from emp where id >= 3 and id <= 6;
select * from emp where id between 3 and 6;
上述语句完全等价
select id,name from emp where salary = 20000 or salary = 18000 or salary = 17000;
select id,name from emp where salary in (20000,18000,17000);
上述语句完全等价
模糊匹配:like
%:匹配多个任意字符
_:匹配一个任意字符
select name,salary from emp where name like ‘%o%‘;
_:匹配一个任意字符
select name,salary from emp where name like ‘____‘;
select * from emp where id < 3 or id > 6;
select * from emp where id not between 3 and 6;
select id,name from emp where salary not in (20000,18000,17000);
select name,post from emp where post_comment = null; # 错误写法,报错
select name,post from emp where post_comment is NULL; # MySQL对大小写不敏感
select * from emp group by post; # 严格模式下会报错
分组之后应该做到最小单位是组,而不应该再展示组内的单个数据信息
MySQL中分组之后,只能拿到分组的字段信息,无法直接获取其他字段信息
但是你可以通过其他方法(聚合函数)简介的获取
如何设置
show variables like ‘%mode%‘;
set session 当前窗口有效
set global 全局有效
set global sql_mode="strict_trans_tables,only_full_group_by";
select * from emp group by post; # 设置严格模式后输入,会报错
select id,name from emp group by post; # 报错
select name from emp group by post;
需要用到聚合函数:max、min、avg、sum、count
select post,max(salary) from emp group by post;
select post as ‘部门‘,max(salary) as ‘最高工资‘ from emp group by post;
select post ‘部门‘,max(salary) ‘最高工资‘ from emp group by post;
两种都可以,推荐使用第一种,结构清晰
select post,min(salary) from emp group by post;
select post,avg(salary) from emp group by post;
select post,sum(salary) from emp group by post;
select post,count(age) from emp group by post; # 可以
select post,count(salary) from emp group by post; # 可以
select post,count(id) from emp group by post; # 可以,且推荐使用
select post,count(post_comment) from emp group by post; # 空字段不行
在统计分组内个数的时候,括号内填写任意非空字段都可以完成计数
推荐使用能够唯一标识数据的字段,比如id字段
select post,group_concat(name) from emp group by post;
select post,group_concat(‘DSB‘,name) from emp group by post; # 不仅可以用来显示除分组外字段,还有拼接字符串的作用
select post,group_concat(name,": ",salary) from emp group by post; # 拼接字符串
select concat("NAME: ",name),concat("SAL: ",salary) from emp;
concat就是用来帮你拼接数据的
concat:不分组情况下使用
group_concat:分组之后使用
select name,salary*12 from emp;
刚开始查询表,一定要按照最基本的步骤,先确定是哪张表,再确定查这张表有没有限制条件,再确定是否需要分类,最后再确定需要什么字段对应的信息
你应该将每一步操作产生的结果都当成是一张新的表,然后基于该表再进行其他的操作
聚合函数 max min sum count avg只能在分组之后使用
如果一张表没有写group by,默认所有的数据就是一组
先获取年轻在30岁以上的员工
select * from emp where age > 30;
再分组求平均工资
select post,avg(salary) from emp where age > 30 group by post;
跟where是一模一样的,也是用来筛选数据的
但是having是跟在group by之后的
where是对整体数据做一个初步的筛选
而having是对分组之后的数据再进行一次针对性的筛选
select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;
select post,avg(salary) from emp where age > 30 group by post where avg(salary) > 10000; # 报错
having必须在group by后面使用
select * from emp having avg(salary) > 10000; # 报错
from
where
group by
having
select
对重复的数据进行一个去重
去重必须数据是一模一样的才能去重
只要有一个不一样,都不能算是的重复的数据
select distinct age from emp;
from
where
group by
having
select
distinct
select * from emp order by salary; # 默认升序排
select * from emp order by salary asc;
上下等价
select * from emp order by salary desc; # 降序排
select * from emp order by age,salary;
select * from emp order by age asc,salary desc;
select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary);
select * from emp limit 5; # 只展示数据的五条
select * from emp limit 5,5;
当limit只有一个参数的时候,表示的是只展示几条
当limit有两个参数的时候,第一个参数表示的是起始位置,第二个参数表示从起始位置开始往后展示的条数
(1)先按照薪资排序
(2)再用limit限制,只取一条
select * from emp order by salary desc limit 1;
在编程中,只要看到reg开头的,基本上都是跟正则相关
select * from emp where name regexp ‘^j.*(n|y)$‘;
select * from emp,dep; # 左表一条记录与右表所有记录都对应一遍>>>笛卡尔积
将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据
select * from emp,dep where emp.dep_id = dep.id;
select * from emp,dep where emp.dep_id = dep.id and dep.name = ‘技术‘;
select * from emp inner join dep on emp.dep_id = dep.id;
select * from emp inner join dep on emp.dep_id = dep.id where dep.name = "技术";
select * from emp left join dep on emp.dep_id = dep.id;
select * from emp right join dep on emp.dep_id = dep.id;
只要将左连接和右连接的sql语句,加一个union就变成全连接
select * from emp left join dep on emp.dep_id = dep.id
union
select * from emp right join dep on emp.dep_id = dep.id;
就是将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用
先获取技术部和人力资源部的id号,再去员工表里面根据前面的id筛选出符合要求的员工信息
select * from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源");
思路:先查每个部门最新入职的员工,再按部门对应上联表查询
select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1 inner join (select post,max(hire_date) as max_date from emp group by post) as t2 on t1.post = t2.post where t1.hire_date = t2.max_date ;
可以给表起别名
可以给查询出来的虚拟表起别名
可以给字段起别名
原文:https://www.cnblogs.com/francis1/p/11391675.html