三、MySQL必备查询知识

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值