mysql学习总结

这篇博客总结了MySQL的各种操作技巧和函数使用,包括时间差计算、时间间隔统计、重复记录查询、多表查询(如内连接、外连接)、SQL执行顺序、日期函数(如DATEDIFF、TIMESTAMPDIFF)、类型转换和数据处理函数(如IFNULL、TRUNCATE)。还介绍了如何统计分组数量、处理重复字段、查询特定时间范围内的数据以及优化查询性能等实用技巧。
摘要由CSDN通过智能技术生成

1、MySql 如何返回 时间差的平均值

//返回平均值为小时
SELECT AVG(TIMESTAMPDIFF(HOUR,submitTime,editTime)) as timediff from orders where editTime is not null;

//返回平均值为分钟
SELECT AVG(TIMESTAMPDIFF(MINUTE,submitTime,editTime)) as timediff from orders where editTime is not null;

//返回平均值为秒
SELECT AVG(TIMESTAMPDIFF(SECOND,submitTime,editTime)) as timediff from orders where editTime is not null;

https://blog.csdn.net/xzy565143480/article/details/90550871

2、MySql以一定的时间间隔统计

func (i *FlowInstanceBuidler) GetTrendChartByInterval(intervalType string, limit float64, whereCond string,fieldName string) (flowCountInfos []FlowTrendCount) {
   
   var sql string
   var fromSql string
   fieldSql := "flow_id,count(*) as nums"
 if whereCond != "" {
   
      whereCond += fmt.Sprintf("and DATE_SUB(CURDATE(), INTERVAL %d %s) <= date(%s) ", int(limit), intervalType,fieldName)
   } else {
   
      whereCond = fmt.Sprintf("where DATE_SUB(CURDATE(), INTERVAL %d %s) <= date(%s) ", int(limit), intervalType,fieldName)
   }
   fromSql = fmt.Sprintf("FROM flow_instance %s GROUP BY internal,flow_id ORDER BY internal DESC ", whereCond)

   switch intervalType {
   
   case "day":
      sql = fmt.Sprintf("SELECT %s, date_format(%s, '%%Y%%m%%d') internal %s ;", fieldSql,fieldName,fromSql)
   case "week":
      sql = fmt.Sprintf("SELECT %s, date_format(%s, '%%x%%v') internal %s ", fieldSql,fieldName,fromSql
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值