【跟上黑马学sql】学习笔记-MySQL-DQL-条件查询

这篇博客介绍了如何使用MySQL进行DQL(数据查询语言)的条件查询,包括查询年龄等于特定值、范围查询、空值判断、性别与年龄组合查询、使用IN操作符和LIKE操作符等实例。
摘要由CSDN通过智能技术生成

   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;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值