sql基础

本文介绍了SQL查询的基本语法,包括使用等值、关系、逻辑和集合比较进行数据过滤,利用BETWEEN、LIKE进行范围和模糊查询,以及ISNULL操作处理空值。此外,还详细讲解了ORDERBY进行排序和COUNT、SUM、AVG、MAX、MIN等聚合函数在数据统计中的应用。
摘要由CSDN通过智能技术生成

Sql语法

-- 选择排序  先执行from,再执行where,再执行select

-- 1.等值比较 =  !=

select * from t_shop where s_price = 4

-- 2.关系比较< >

select * from t_shop where s_price > 2

-- 3.逻辑关系 and or 

select * from t_shop where s_price > 2 and s_name like '_瓜'

select * from t_shop where s_price > 2 or s_name like '_瓜'

-- 4.范围比较 between and   (时间范围)

select * from t_shop where s_price BETWEEN 1 and 5

select * from `机架式服务器` where `邮寄日期` between '2018-04-12' and NOW()

-- 5.集合比较 in not in

select * from t_shop where s_price in(2,4,6)

select * from t_shop where s_price not in(2,4,6)

-- 6.模糊查询 like (%任意字符) (_单字符

select * from t_shop where s_name like '_瓜'

-- 7.空值比较is null 查询为空的  in not null 查询不为空的

select * from `机架式服务器` where f24 is null

select * from `机架式服务器` where f24 is not null

--8.排序操作 desc降序 asc升序

select * from t_shop ORDER BY s_id desc

select * from t_shop ORDER BY s_id asc

select * from t_shop ORDER BY s_price asc,s_id desc

-- 聚合函数进项数据处理

-- count 统计

-- *统计所有行,包括null值

SELECT count(*) '数据量' FROM `机架式服务器`

-- all列 统计指定的所有非空值行

select count(all `序号`) '数据量' from `机架式服务器`

-- distinct 统计指定列的所有唯一非空值行 (去重)

select count(distinct `邮寄人`) '数据量' from `机架式服务器`

-- sum求和

-- 统计总和 all可省略

select SUM(`数量`) '数量和' from `机架式服务器`

-- distinct 求和非null 并且去重

select SUM(DISTINCT `数量`) '数量和' from `机架式服务器`

-- avg 求平均 avg自身不统计null进行计算  可用ifnull

select avg(`数量`) '平均数'from `机架式服务器`

-- ifnull

select avg(ifnull(`数量`,0)) '平均数'from `机架式服务器`

-- 方法2 sum / 数量

SELECT sum(`数量`) / count(DISTINCT `邮寄人`) '平均数' from `机架式服务器`

-- 求最大最小 max min

-- max 最大

select max(`数量`)'最大' FROM `机架式服务器`-- 不统计null

-- min 最小

select min(`数量`)'最小' FROM `机架式服务器`-- 不统计null

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值