MySQL-DQL-条件查询笔记实例
#创建emp1表
create table emp1
(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(5) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
workaddress varchar(50) comment '工作地址',
entrydate date comment '入职时间'
) comment '员工信息表';
#向表中添加数据
insert into emp1(id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (1, '1', '嫦娥', '女', 30, '123456789012345678', '北京', '2000-01-01'),
(2, '2', '张飞', '男', 18, '123456789012345670', '北京', '2005-09-01'),
(3, '3', '李坤', '女', 38, '123456789012345670', '上海', '2005-08-01'),
(4, '4', '赵路', '女', 18, '123456789012345670', '北京', '2009-12-01'),
(5, '5', '小果', '女', 16, '123456789012345678', '上海', '2007-07-01'),
(6, '6', '杨过', '男', 28, '12345678901234567X', '北京', '2006-01-01'),
(7, '7', '范为', '男', 40, '123456789012345670', '北京', '2005-05-01'),
(8, '8', '卢子侧', '男', 38, '123456789012345670', '天津', '2015-05-01'),
(9, '9', '李凉凉', '女', 45, '123456789012345678', '北京', '2010-04-01'),
(10, '10', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01'),
(11, '11', '张士诚', '男', 55, '123456789012345670', '江苏', '2015-05-01'),
(12, '12', '常遇春', '男', 32, '123456789012345670', '北京', '2004-02-01'),
(13, '13', '张无几', '男', 88, '123456789012345678', '江苏', '2020-11-01'),
(14, '14', '女作家', '女', 65, '123456789012345670', '西安', '2019-05-01'),
(15, '15', '胡时', '男', 70, null, '西安', '2018-04-01'),
(16, '16', '周熟人', '男', 28, null, '河南', '2022-06-01'),
(17, '17', '王字清', '男', 24, null, '河南', '2022-06-01'),
(18, '18', '凤城表', '男',30, null, '河南', '2022-06-01'),
(19, '19', '卢书杰', '男', 31, null, '河南', '2022-06-01'),
(20, '20', '狗证', '男', 26, null, '河南', '2022-06-01');
#条件查询
##1.查询年龄等于18的员工
select * from emp1 where age=18;
##2.查询年龄小于30的员工信息
select * from emp1 where age<30;
##3.查询年龄小于等于30的员工信息
select * from emp1 where age<=30;
##4.查询没有身份证号的员工
select *from emp1 where idcard is null;#千万不要用=null。
##5.查询有身份证号的员工信息;
select *from emp1 where idcard is not null;
##6.查询年龄不等于88的员工信息
##方法一
select *from emp1 where age !=88;
###方法二
select *from emp1 where age <>88;
##7.查询年龄在15岁(包含)到30岁(包含)之间的员工信息
###方法一
select *from emp1 where age>=15 && age<=30;
###方法二
select *from emp1 where age>=15 and age<=30;
###方法三
select *from emp1 where age between 15 and 30;#between跟最小值,and跟最大值,不要写反
##8.查询性别为女且年龄小于40岁的员工信息
select * from emp1 where gender='女' and age<40;
##9.查询年龄等于20或30或40的员工信息
###方法一
select * from emp1 where age=20 or age=30 or age=40;
###方法二
select * from emp1 where age in(20,30,40);#in(,,)代表满足中间其一即可,多选一
##10.查询姓名为两个字的员工信息 _ %
select *from emp1 where name like '__';#中间不用空格
##11.查询身份证号最后一位是x的员工信息
###方法一
select *from emp1 where idcard like '%X';# %代表任意的字符
###方法二
select *from emp1 where idcard like '_________________X';#一个下划线代表一个字符:17个_+X;