持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第9天,点击查看活动详情
基本语法 DQL 查询语句
本次案例所用到的表
查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息
3种查询方式
select * from emp where age >= 15 && age <= 20;select * from emp where age >= 15 and age <= 20;select * from emp where age between 15 and 20;
查询性别为女且年龄小于25岁的员工信息
select * from emp where gender = '女' and age < 25
查询年龄等于18 或 20 或 40 的员工信息
select * from emp where age = 18 or age = 20 or age =40;select * from emp where age in(18,20,40);
查询姓名为两个字的员工信息 _ %
select * from emp where name like '__';
查询身份证号最后一位是X的员工信息
select * from emp where idcard like '%X';select * from emp where idcard like '_________________X';
聚合函数
常见的聚合函数
语法
SELECT 聚合函数(字段列表) FROM 表名 ;- 注意 : NULL值是不参与所有聚合函数运算的。
案例:
统计该企业员工数量
select count(*) from emp;-- 统计的是总记录数select count(idcard) from emp;-- 统计的是idcard字段不为null的记录数- 对于count聚合函数,统计符合条件的总记录数,还可以通过 count(数字/字符串)的形式进行统计
查询,比如:
select count(1) from emp; - 一般情况下,Select Count ()和Select Count(1)两着返回结果是一样的如果表中没有主键 ,使用count(1)比count()快;如果有主键,那么count(主键)最快count(*)和count(1)的结果一样,都包括对NULL的统计,而count(字段) 不包括NULL的统计;实操中,选择使用 count(1)的情况比较多