MySQL SQL基础DQL查询

我们先把表和数据准备好

-- 数据准备
create table emp(
    id          int                 comment '编号',
    workno      varchar(10)         comment '工号',
    name        varchar(10)         comment '姓名',
    gender      char(1)                comment '性别',
    age         tinyint unsigned    comment '年龄',
    idcard      varchar(18)         comment '身份证号',
    workaddress varchar(50)         comment '工作地址',
    entrydate   date                comment '入职时间'
)comment '员工表';

 基础查询

1、查询指定字段 name,workno,age 返回
select name,workno,age from emp;
2、查询所有字段返回
select id,workno,gender,age,idcard,workaddress,entrydate from emp;

select *from emp;    -- 推荐使用上面那种方式
3、查询所有员工的工作地址,起别名
select workaddress as '工作地址' from emp;
select workaddress  '工作地址' from emp;    -- 加不加as无所谓
4、查询公司员工的上班地址(不要重复)
select distinct workaddress '工作地址' from emp;    -- distinct关键字用来去除重复

distinct关键字要注意

条件查询

-- 条件查询
-- 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;

-- 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;
select *from emp where age between 15 and 20;

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

-- 9.查询年龄等于18或20或48的员工信息
select* from emp where age=18 or age=20 or age=40;
select* from emp where age in(18,20,40);

-- 10.查询姓名为两个字的员工
select *from emp where name like '__';      -- 两个下划线表示两个字

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

为了方便,我们全部使用select*,select*表示整个表所有的字段。剩下的就没什么好讲的了,自己看看就好的说。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值