SQL——DQL用来查询数据库表中的记录(MySQL)

DQL 英文全称是 Data Query Language( 数据查询语言 ),用来查询数据库表中的记录。

关键字:select

一、前言

select    字段列表   from 表名列表 --基本查询

where    条件列表-----条件查询(where)

group by  分组字段列表  having 分组后条件列表--分组查询(group by)

order by   排序字段列表----排序查询(order by)

limit       分页参数----分页查询(limit)  

二、基本查询

(一)、语法

 (1)查询多个字段:select 字段1, 字段2, 字段3  from 表名;

 (2)查询所有字段(通配符):select * from 表名;

 (3)为字段设置别名:select 字段1[ as 别名1 ], 字段2[ as 别名2 ] from 表名;

 (4)去除重复记录:select distinct 字段列表 from 表名;

(二)、相应例子以及对应注意事项

 (1)查询指定字段

select name,entrydate from tb_emp;

 (2)查询所有字段

-- 推荐使用
select id, username, password, name, gender, image, job, entrydate, create_time, update_time from tb_emp;
-- 不推荐 不直观,性能低
select *from tb_emp;

注:通配符*号代表查询所有字段,在实际开发中尽量少用(不直观、影响效率)。

 (3)查询所有员工的name 、entrydate,并起别名(姓名、入职日期)

select name as 姓名,entrydate as 入职日期 from tb_emp;-- 别名后加空格,与from分开 否则会认为from为别名一部分
select name as '姓名',entrydate as '入职日期'from tb_emp;
select name '姓名', entrydate '入职日期'from tb_emp;-- as后加空格  as可以不加,不加用空格代替

 (4)查询已有员工关联了哪几种职位(不要重复)

select distinct job from tb_emp;

相应建表语句(数据记录中有一个数据记录为null)

job         tinyint unsigned             null comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管',

三、条件查询( where )

(一)、语法

条件查询:select  字段列表  from  表名  where  条件列表;

(二)、运算符及其功能总结

比较运算符功能
>大于
>=大于等于
<小于
<=小于等于
=等于
<>  或  !=不等于
between ... and ...在某个范围之内(含最小、最大值)
in( ... )在in之后的列表中的值,多选一
like  占位符模糊匹配( _ 匹配单个字符,% 匹配任意个字符)
is  null是null

逻辑运算符功能
and  或  &&并且(多个条件同时成立)
or    或  ||或者(多个条件任意一个成立)
not  或  !非 ,不是

(三)、例子

-- ---------条件查询-----------
-- 1、查询姓名为杨逍的员工
select *from tb_emp where name= '杨逍';-- 要用''、 不能用""——名字为字符串

-- 2、查询id小于等于5
select *from tb_emp where id<=5;

-- 3、查询 没有 分配职位 的员工信息(job=null)
select *from tb_emp where job is null;

-- 4、 查询有职位的员工信息
select *from tb_emp where job is not null;

-- 5、查询 密码不等于’123456‘ 的员工信息
select *from tb_emp where password !='123456';
select *from tb_emp where password <> '123456';

-- 6、查询 入职日期 在’2000-01-01‘(包含) 到 ’2010-01-01‘(包含) 之间 的员工信息
select *from tb_emp where entrydate>='2000-01-01'  and entrydate<='2010-01-01';
select *from tb_emp where entrydate between '2000-01-01'  and '2010-01-01';

-- 7、查询 入职日期 在’2000-01-01‘(包含) 到 ’2010-01-01‘(包含) 之间 且性别为女 的员工信息
select *from tb_emp where entrydate between '2000-01-01'  and '2010-01-01' and gender=2;

-- 8、查询 职位是 2(讲师)或 3(学工主管)或 4(教研主管) 的员工信息
select *from tb_emp where job=2 or job=3 or job=4;
select *from tb_emp where job in(2,3,4);

-- 9、查询 姓名 为两个字 的员工信息
select *from tb_emp where name like '__';

-- 10、查询 姓 ’张‘ 的员工信息 即查询第一个字符为’张‘的
select *from tb_emp where name like '张%';-- 查询第一个姓张 后面无所谓即后面为任意个字符

四、分组查询( group by )

(一)、聚合函数

1、聚合函数语法

 (1)介绍:将一列数据作为一个整体,进行纵向计算

 (2)语法:select  聚合函数( 字段列表 )  from  表名;

函数功能
count统计数量
max最大值
min最小值
avg平均值
sum求和

2、注意事项

 (1)null值不参与所有聚合函数运算

 (2)统计数量有三种方法:count(*)、count(字段)、count(常量),推荐使用count(*)

 (3)count(*): *是通配符,通过count(*)就可以求取表的总数据量, 推荐使用——MySQL底层专门做了优化处理

 (4)用count函数统计表的数据量时一定要count一个非空字段(not null)

3、例子

-- 1、统计员工数量-----count
-- A、count(字段)----用count函数统计表的数据量时一定要count一个非空字段(not null)
select count(id) from tb_emp;
select count(job) from tb_emp;

-- B、count(常量)---count一个不为null的常量都行
select  count(1) from tb_emp;
select  count('a' ) from tb_emp;

-- C、count(*) *是通配符  
select count(*) from tb_emp;

注注:tb_emp表id为not null, job为null,且有条数据job=null

    count(id)                                       count(job)

                 

用count函数统计表的数据量时一定要count一个非空字段(not null),因为聚合函数不对null进行计算,所以表中有两条数据job为null不参与运算。

-- 2、统计该企业最早入职的员工---min
select  min(tb_emp.entrydate) from tb_emp;

-- 3、统计该企业最迟入职的员工--max
select max(tb_emp.entrydate) from tb_emp;

-- 4、统计该企业员工id的平均值 --avg
select avg(tb_emp.id) from tb_emp;

(二)分组查询语法

1、语法

分组查询:select 字段列表 from 表名 [ where 条件 ] group by 分组字段名 [ having 分组后过滤条件 ];

2、where+条件和having+条件区别

(1)执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤。

(2)判断条件不同:where不能对聚合函数进行判断(即where后不能跟聚合函数),而having可以

3、注意事项

(1)分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义。

(2)执行顺序:where>聚合函数>having

4、例子

(1)分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义。

根据性别分组,统计男性和女性员工的数量——count(*)

select * from tb_emp group by gender;            select id from tb_emp group by gender;

                    

无意义

select gender from tb_emp group by gender;
select gender,count(*) from tb_emp group by gender;

            

分组之后,以gender为条件分为1和2两组(即男和女),对这两组进行select  gender和count统计

(2)执行顺序:where>聚合函数>having

先查询入职时间在'2015-01-01'(包含) 以前的员工,并对结果根据职位分组,获取员工数量大于等于2的职位

select job,count(*) from tb_emp where entrydate<='2015-01-01' group by job having count(*)>=2;

注:首先执行where+条件,然后满足条件的数据执行分组语句和聚合函数,最后执行having+条件进行分组后条件的过滤。

五、排序查询( order by )

(一)、语法

排序查询:select 字段列表 from 表名 [ where 条件列表 ] [ group by 分组字段 ] order by 字段1 排序方式1,字段2 排序方式2;

(二)、排序方式

(1)ASC:升序(默认值)

(2)DESC:降序

(三)、注意事项

如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序。以此类推

(四)、例子

-- 1、根据入职时间,对员工进行升序排序--asc
select *from tb_emp order by entrydate asc;
select *from tb_emp order by entrydate;

-- 2、根据 入职时间 对员工进行升序排序,入职时间相同,再按照 创建时间和更新时间 进行降序排序
select * from tb_emp order by entrydate,create_time desc ,update_time desc;

六、分页查询( limit )

(一)、语法

分页查询:select 字段列表 from 表名 limit 起始索引, 查询记录数;

(二)、注意事项

(1)起始索引从0开始,起始索引=(查询页码-1)*每页显示的记录数

(2)分页查询是数据库的方言(即数据库所特有的),不同数据库有不同的实现,MySQL中是LIMIT

(3)如果查询的是第一页数据,起始索引可以省略,直接简写成 limit 10( 10为每页显示的记录数)

(三)、例子

-- 1、查询 第一页 的员工数据,每页展示5条记录
select * from tb_emp limit 0,5;
select * from tb_emp limit 5;

-- 2、查询 第三页 员工数据,每页展示5条记录;
select * from tb_emp limit 10,5;

         

  • 22
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值