数据库006

本文深入探讨了SQL查询中的模糊查询、过滤空数据、排序、聚合函数、数据分组等核心技巧,以及如何有效进行数据处理。通过具体示例,帮助读者掌握SQL查询的高级用法。

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

1.模糊查询 
%    包含零个或更多字符的任意字符串
_     任何单个字符
select  *  from   t_class   where   c_add   like   ’%路%’;#表示查询表中的地址中包含有“路”字的数据。
select   *  from   t_class   where   c_add   like   '路%'; #表示查询表中的地址以“路”字开头的数据。
select   *  from   t_class   where   c_add   like   '%路'; #表示查询表中的地址以“路”字结尾的数据。
select   *  from   t_class   where   c_name    like   '__'; #表示查询表中的名字是两个字的数据。
2.过滤空值数据
select   *  from   t_class   where   c_age    is /is  not  null;  #表示查询表中的年龄是空/不是空的数据。
3.升序排列asc 和降序排列 desc
select   *  from   t_class   where   c_age   is  not null  order by c_age;  #表示查询表中的年龄以升序排列的数据。
select   *  from   t_class   where   c_age   order by c_name desc;  #表示查询表中的年龄和名字以降序排列的数据。
4.聚和函数:用于查询统计数据。
count 对行计数,包括空值。
select  count (<计数规范>)from 表名
select count (s_id)  from t_class; #表示计算s_id的行数。
select  count (distinct  s_name)  from   t_student;
排除重复数据和非空数据。
select sum (all  s_age )from   t_student ;
聚合函数sum求某列数据的总和。
select avg (all  s_age )from t_student ;#求平均值
ps :avg不会计算空值,如果十个人中有一个人的数据是空值,那么计算的时候就会只计算9个人的值,计算出来的结果可能跟自己想要的不一样。可以用sum/count(*)的方式来自己计算。
5.Max返回最大值 Min返回最小值
select max (s_age)  from t_student ;
select  min (s_age)  from  t_student;
ps:不可加在where后面,不可查询其他列。
数据分组:group by 加在where 条件后分组。
select   s_age count (s_age)  from t_student where s_age >18 group  by     s_age;
先按班分组,然后求每个组学生的总和。
select  s_s_id ,count (s_id)  from t_student group by s_c_id;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值