基本查询
sql
-- 查询id,name,creatdata
select id,name,creatdata from tb_emp;
-- 查询所有值
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp;
select *from tb_emp;-- 不推荐
-- 查询id creatdata,并起一个别名
select id 'ID',creatdata '创建时间' from tb_emp;
-- 查询不重复
select distinct gender from tb_emp;
条件查询
sql
-- 查询姓名为李华的员工
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp where name='李华';
-- 查询员工号小于3的员工
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp where id<3;
-- 查询没有分配职位的员工
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp where zhiwei is null;
-- 查询分配职位的员工
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp where zhiwei is not null ;
-- 查询密码等于123456的员工
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp where mima='123456';
-- 查询日期在2024-3-8(包含)与2024-3-8(包含)并且员工性别为男的员工
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp
where creatdata between '2024-03-8' and '2024-03-9' and gender =1;
-- 查询职工为2和3的员工信息
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp where zhiwei=3 or zhiwei =2;\
-- 查询姓名为两个字的员工
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp where name like '__' ;
-- 查询姓为张的员工
select id, user, name, gender, image, mima, zhiwei, creatdata from tb_emp where name like '张%';
分组查询
sql
-- 聚合运算 --不对null值进行运算
-- 统计员工数量
-- count(字段)
select count(zhiwei) from tb_emp;
-- count(常量)
select count(1) from tb_emp;
-- count(*)
select count(*) from tb_emp;
-- 统计员工最早入职日期
select min(creatdata) from tb_emp;
-- 统计员工最早晚职日期
select max(creatdata) from tb_emp;
-- 求取员工id平均值
select avg(id) from tb_emp;
-- 求取员工id和
select sum(id) from tb_emp;
-- 选取id小于10的字段,在对密码进行的分组,求数量大于2的
select mima,count(*) from tb_emp where id<10 group by mima having count(*)>2;
升序排序
分页查询
sql
-- 查询第1页,每页3条数据
select *from tb_emp limit 0,3;
-- 2页,3条
select *from tb_emp limit 3,3;
-- 起始索引=(页码-1)*记录数