04-Mysql-单表查询【学习笔记】

1 - 简单查询

select 字段1(列名),字段2,字段3,...from表名
mysql> select job,SAL from emp;
//字段可以直接进行数学运算
select sal*12 from emp;
//使用as可以给字段重命名
//注意,新的字段名如果是中文最好用单引号,双引号不通用
select ename,sal*12 as ‘年薪’ from emp;
//查询所有字段
//实际开发中不建议使用* 效率较低 不能写在java程序中
select * from 表名

2 - where条件查询

执行顺序:先from 再where 再select

select sal from emp where ename ='SMITH';

判断是否在某一区间

  1. <> != 表示不等于
  2. between and
-- 找出工资不等于3000的
select ename,sal from emp where sal <> 3000/ sal != 3000

-- 找出工资在1100 和 3000 之间的
select ename,sal from emp where sal between 1100 and 3000//闭区间【1100-3000】

-- between and除了可以使用在数字方面之外,还可以使用在字符串方面。
select ename from emp where ename between 'A' and 'C';

is null 和 is not null
在数据库当中NULL不是一个值,代表什么也没有,为空。空不是一个值,不能用等号衡量。必须使用 is null或者is not null

//找出津贴不为null的人
select ename,sal,comm from emp where comm is not null;

//找出没有津贴的人
select ename,sal,comm from emp where comm is null or comm = 0;

or 和 and的使用
and 的优先级大于or

select ename,sal,deptno from emp where sal > 1000 and (deptno = 20 or deptno = 30);

in 和 not in 的使用
注意这是两个数选一个 而不是一个区间范围!

select ename,job from emp where sal not in(800, 5000);

3 - 模糊查询

%:多个字符 _:一个字符

//找出名字中第二个字母是A的?
select ename from emp where ename like '_A%';
//找出名字有下划线的
//此处下划线代表任意一个字符,所以需要转义
select name from t_user where name like '%\_%';

4 - 排序

order by asc表示升序,desc表示降序
默认升序

select ename , sal from emp order by sal; // 升序

select ename , sal from emp order by sal asc; // 升序

select ename , sal from emp order by sal desc; // 降序。

越靠前的字段越能起到主导作用。只有当前面的字段无法完成排序的时候,才会启用后面的字段。

select ename,sal from emp order by sal desc , ename asc;

5 - 分组函数

又叫多行处理函数:输入多行,最终输出的结果是1行。
count 计数

sum 求和

avg 平均值

max 最大值

min 最小值

找出工资总和?

        select sum(sal) from emp;

找出最高工资?

        select max(sal) from emp;

找出平均工资?

        select avg(sal) from emp;

找出总人数?

        select count(*) from emp;

        select count(ename) from emp;

count()和count(具体的某个字段),他们有什么区别?
count(
):不是统计某个字段中数据的个数,而是统计总记录条数。(和某个字段无关)
count(comm): 表示统计comm字段中不为NULL的数据总数量。

注意:
1.分组函数自动忽略null

select sum(comm) from emp;

select sum(comm) from emp where comm is not null; // 不需要额外添加这个过滤条件。sum函数自动忽略NULL。

2.分组函数不能直接用在where里

 //ERROR 1111 (HY000): Invalid use of group function
   select ename,sal from emp where sal > avg(sal);

ifnull 空处理函数
所有数据库都是这样规定的,只要有NULL参与的运算结果一定是NULL。
ifnull(可能为NULL的数据,被当做什么处理) : 把所有是null的数据都赋值为0就可以进行运算了

select ename,ifnull(comm,0) as comm from emp;

group by 和 having

group by : 按照某个字段或者某些字段进行分组。
having : having是对分组之后的数据进行再次过滤。
记住一个规则:当一条语句中有group by的话,select后面只能跟分组函数和参与分组的字段。

//每个工作岗位的平均薪资?

select job,avg(sal) from emp group by job;

多字段联合分组,相当于把两个字段拼接成一个字段
deptno+job

10 | CLERK == 10CLERK

//找出每个部门不同工作岗位的最高薪资。
select 

        deptno,job,max(sal)

from

   emp

group by

    deptno,job;

having的使用
必须先分组再过滤的用having 不用where,因为where后面不能写分组函数

找出每个部门的平均薪资,要求显示薪资大于2000的数据。

    第一步:找出每个部门的平均薪资

    select deptno,avg(sal) from emp group by deptno;

    

    第二步:要求显示薪资大于2000的数据

    select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000; 

6 - DQL执行顺序

select      5

        ..

    from            1   

        ..

    where           2

        ..

    group by        3

        ..

    having      4

        ..

    order by        6

        ..

可以缺省,但是顺序不能换!!!!!!!!!!!!!

7 - 查询结果去重

-- distinct关键字去除重复记录。
select distinct job from emp; 
select distinct deptno,job from emp;

-- 统计岗位的数量?
select count(distinct job) from emp;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容