2.4 DQL-条件查询丶聚合函数

本文介绍了SQL查询的基础语法,包括选择字段、使用不同条件(如等于、不等于、区间、空值等)进行过滤,以及运用聚合函数如COUNT、AVG、MAX、MIN和SUM对数据进行统计分析。同时,提到了LIKE操作符用于模糊匹配和IN操作符处理多个条件的情况。
摘要由CSDN通过智能技术生成

1丶语法

select 字段列表 from 表名 where 条件列表

2丶条件

-- 条件查询
-- 1丶查询年龄等于 88 的员工
select * from emp where age = 88;

-- 2丶查询年龄小于 20 的员工信息
select * from emp where age <20;

-- 3丶查询年龄小于等于 20的员工信息
select *from emp where age <=20;

-- 4丶查询没有身份证号的员工信息
select *from emp where idcard is null; -- is null 是 null

-- 5到丶查询有身份证好的员工信息
select *from emp where idcard is not null;

-- 6丶查询年龄不等于 88 的员工信息
select * from emp where age !=88; -- 第一种查询方式
select * from emp where age <>88; -- 第二种查询方式

-- 7丶查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息
select *from emp where age >=15 && age<=20;
select *from emp where age >=15 and age<=20; -- and - 并且

select *from emp where age between 15 and 20; -- between~and 代表区间

-- 8丶查询性别为 女 且年龄小于 25岁的员工信息
select *from emp where gender ='女'and age<25;

-- 9丶查询年龄等于18 或 28 或 40 的员工信息
select * from emp where age =18 or age=28 or age=40; -- or - 或
select * from emp where age in(18,28,40); -- in(值1,值2,...)满足多个条件同时存在

-- 10丶查询姓名为两个字的员工信息 _ %
select * from emp where name like '__'; -- like - 模糊匹配

-- 11丶查询身份证号最后一位是 x 的员工信息
select * from emp where idcard like '%x';
select * from emp where idcard like '_________________________[x';

 

2丶DQL - 聚合函数

1丶介绍

将一列数据作为一个整体,进行纵向计算

2丶常见聚合函数

 3丶语法

select 聚合函数(字段列表) from 表名;

注意:null值不参与所有聚合函数运算

-- 聚合函数
-- 1丶统计该企业员工数量
select count(*) from emp;
select count(idcard) from emp; -- null值不参与所有聚合函数运算

-- 2丶统计该企业员工的平均年龄
select avg(age) from emp;

-- 3丶统计该企业员工的最大年龄
select max(age) from emp;

-- 4丶统计该企业员工的最小年龄
select min(age) from emp;

-- 5丶统计北京地区员工的年龄之和
select sum(age) from emp where workaddres = '北京';

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值