-- 一、条件查询
select * from info;
-- 查询info表中年龄大于30的记录
select * from info where age>30;
-- 查询info表中id大于1的数据
select * from info where id>1;
-- 查询info表中id等于1的数据
select * from info where id=1;
-- 查询info表中id大于等于3的数据
select * from info where id >= 3;
-- 查询info表中不但能于4的数据
select * from info where id!=4;
-- 查询info表中id大于2,小于5的数据
select * from info where id >2 and id<5;
select * from info where id between 3 and 4;
-- 查询info表中name=liuag,age等于23的数据
select * from info where name='liang' and age=23;
-- 查询info表中name等于1或者age等于21的数据
select * from info where name='liang' or age=21;
-- 查询name=liang或者邮箱为ll@163.com和age=23的数据
select * from info where (name='liang' or email='ll@163.com') and age=23;
-- 查询id为4,5,6的记录
select * from info where id in (4,5,6);
select * from info where id not in(4,5,6);
select * from info where id in (select id from depart);
-- 去查数据是否存在,如果存在,如果不存在。
select * from info where exists (select * from depart where id=5);
--
select * from (select * from info where id>2) as T where age>30;
-- 二、通配符查询
select * from info where name like '%l';
select * from info where name like 'li%';
select * from info where name like 'l_';
-- 三、映射查询
select name, age from info;
select
id,
name,
666 as num,
( select max(id) from depart ) as mid, -- max/min/sum
( select min(id) from depart) as nid, -- max/min/sum
age
from info;
select
id,
name,
case depart_id when 1 then "第1部门" end v1,
case depart_id when 1 then "第1部门" else "其他" end v2,
case depart_id when 1 then "第1部门" when 2 then "第2部门" else "其他" end v3,
case when age<18 then "少年" end v4,
case when age<18 then "少年" else "油腻男" end v5,
case when age<18 then "少年" when age<30 then "青年" else "油腻男" end v6
from info;
-- 四、查询排序
select * from info order by name asc;
select * from info order by name desc;
select * from info order by age asc,id desc; -- 优先按照age从小到大;如果age相同则按照id从大到小。
-- 五、取部分值
select * from info limit 3;
select * from info order by id asc limit 4;
-- 六、分组分组
select age,max(id),min(id),count(id),sum(id),avg(id) from info group by age;
select age, count(1) from info group by age;
select depart_id, count(id) from info group by depart_id;
select depart_id,count(id) from info group by depart_id;
-- 分组加条件
select * from info;
select depart_id, count(id) from info group by depart_id having count(id) > 1;
select depart_id,count(id) from info group by depart_id having count(id) > 2;
select age,max(id),min(id),sum(id),count(id) from info group by age;
select age,name from info group by age; -- 不建议
select max(id) from info group by age;
select * from info where id in (select max(id) from info group by age);
-- 聚合条件放在having后面
select age, count(id) from info where id >4 group by age;
select age, count(id) from info group by age having count(id) > 2;
-- 七、连接查询
select * from info left join depart d on info.depart_id = d.id;
select info.id, info.name, info.email,d.title from info
left join depart d
on info.depart_id = d.id;
insert into depart(title) values("运维");
-- 从表 right outer join 主表 on 主表.x = 从表.id
select info.id,info.name,info.email,depart.title from info
right outer join depart
on info.depart_id = depart.id;
create database db03 default charset utf8 collate utf8_general_ci;
三、MySQL必备查询知识
最新推荐文章于 2023-06-21 14:59:13 发布