数据库学习记录

SQL语句学习

函数

group by

select 
	from_id as person1, 
	to_id as person2, 
	count(duration) call_count, 
	sum(duration) total_duration from Calls 
group by least(from_id, to_id), greatest(from_id, to_id);

least

求最小值:least()函数返回在该组输入参数(N1,N2,N3,等等)的最小值

greatest

求最大值:greatest()函数返回在该组输入参数(N1,N2,N3,等等)的最大值

count

统计次数:统计某个字段的出现次数

sum

求和: 对某个字段求和
可以通过sum配合if来判断某个字段的出现次数。如下,统计表中product_name出现过的次数

sum(if(product_name = 'A', 1, 0)) 
sum和count的区别

count() does not care about the content inside it, as long as the content is NOT NULL. Because count(1) and count(0) are still values and both get counted.
count 只关心当下的括号内的值是不是null,不关心是0还是1,0和1都一样都不是null。
sum是会关心是0还是1,因为需要做累加。
以下三种写法均可以:

#统计表中 order_date 和 customer_pref_delivery_date相同的条数

sum(if(order_date = customer_pref_delivery_date, 1, 0))
count(case when order_date = customer_pref_delivery_date then 1 else NULL end)
count(if(order_date = customer_pref_delivery_date, 1, NULL)
</
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值