mysql离散查询_MYSQL 查询

本文详细介绍了SQL查询的基本语法,包括选择所有数据、特定列、条件过滤、排序、聚合函数、分页、分组、去重、改列名、模糊匹配和离散查询。还涉及到了连接查询、联合查询、子查询等高级技巧,帮助读者全面掌握SQL的数据检索能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查询:

1.普通查询,查所有

select * from info 查所有数据

select code,name from info 查指定列

2.条件查询

select * from info where code='p001' 一个条件

select * from info where name='张三' and nation='n001' 并关系

select * from info where name='张三' or nation='n001' 或关系

3.排序查询

select * from info order by birthday 默认升序排列(asc)降序(desc)

select * from car order by brand,oil desc 多列排序

4.聚合函数

select count(*) from info 取个数

select sum(price) from car 查询price列的和

select avg(price) from car 查询price列的平均值

select max(price) from car 查询price列的最大值

select min(price) from car 查询price列的最小值

5.分页查询

select * from car limit 0(跳过几条数据),5(取几条数据)

6.分组查询

select brand from car group by brand 简单分组查询

select brand from car group by brand having count(*)>2 查询系列里面车的数量大于2的系列

7.去重查询

select distinct brand from car 去重

8.修改列名

select brand as '系列' from car

9.模糊查询

select * from car where name like '_奥迪%' %表示任意多个字符(包含%***%)(_代表一个字符)

10.离散查询

select * from car where code in('c001','c002','c003','c004')

select * from car where code not in('c001','c002','c003','c004')

查询汽车表中 价格大于50 40 39 这三个数中最大的

select * from car where prise >50

高级查询:

1.连接查询:

select * from info,nation 得出的结果称为笛卡尔积,效率低

select * from info,nation where info.nation = nation.code

join on连接:

select * from info join nation join连接

select * from info join nation on info.nation = nation.code

2.联合查询:

select code,name from info

union

select code,name from nation

3.子查询:

1)无关子查询:

select code from nation where name='汉族' 查询nation表中汉族的民族代号

select * from info where nation = () 在info表中查询民族代号为上一个结果查询的民族代号

select * from info where nation = (select code from nation where name='汉族')

子查询查询的结果被父查询使用,子查询可以单独执行的称为无关子查询

2)相关子查询:

select * from car where oil

select avg(oil) from car where brand='值' 查询某系列的平均油耗

select * from car a where oil

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值