前言

使用 gorm 查询时,报错:stdErr = sql: Scan error on column index 0, name "total": converting NULL to float64 is unsupported

代码如下

var total float64
res := db.Model(&model.Record{}).Select("sum(amount) as total").Where("id = ? and type = ? ", key.Id, key.Type).Find(&total)
if res.Error != nil {
return 0, errors.Errorf(res.Error, errors.ErrorDBFindFailed)
}

这个错误是因为查询结果中的 total 列的值为 NULL,而尝试将 NULL 转换为 float64 类型时发生了错误。

查询结果中的 total 列的值为 NULL 可能有多种原因:

  1. 数据库中没有匹配的记录:根据查询条件,可能没有符合条件的记录存在,因此返回的"total"列的值为 NULL。这意味着在数据库中没有与给定的 idtype 匹配的记录。
  2. amount 列的值为 NULL:如果 amount 列的值为 NULL,那么使用 SUM 函数计算总和时,结果将为 NULL。因此,total 列的值将为 NULL
  3. 数据库中存在 NULL值:如果 amount 列中存在 NULL 值,并且在查询中没有处理 NULL 值的情况,那么计算总和时将返回 NULL

解决方法

import "database/sql"

var total sql.NullFloat64
res := db.Model(&model.Record{}).Select("COALESCE(sum(amount), 0) as total").Where("id = ? and type = ? ", key.Id, key.Type).Find(&total)
if res.Error != nil {
return 0, errors.Errorf(res.Error, errors.ErrorDBFindFailed)
} if total.Valid {
return total.Float64, nil
} else {
return 0, nil
}

在上述代码中,使用了 SQL 的 COALESCE 函数将 sum(amount) 的结果替换为0(或其他默认值)。

然后,我们检查 total.Valid 字段,如果查询结果不为 NULL ,则将其转换为 float64 类型并返回。

通过这种方式,即使查询结果中的"total"列的值为NULL,也可以正常处理,并且可以返回默认值或根据业务逻辑进行处理。

gorm stdErr = sql: Scan error on column index 0, name "total": converting NULL to float64 is unsupported的更多相关文章

  1. Scan error on column index 1, name “created_at“: unsupported Scan, storing driver.Value type []uint8

    使用gorm,出现以下报错 在连接数据库时加上: parseTime=True db, err = gorm.Open(utils.Db, fmt.Sprintf("%s:%s@(%s:%s ...

  2. 表结构变更后出现的ERROR OGG-01161 Bad column index (88)

    2014-07-31 09:38:31 ERROR OGG-01668 PROCESS ABENDING. 2014-07-31 09:38:31 ERROR OGG-01161 Bad column ...

  3. java.sql.SQLException:Column Index out of range,0<1

    1.错误描述 java.sql.SQLException:Column Index out of range,0<1 2.错误原因 try { Class.forName("com.m ...

  4. org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup' from result set. Cause: java.sql.SQLException: Error

    异常展示: org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup ...

  5. Union比or快 Using UNION is faster when it comes to cases like scan two different column。

    problem: 595. Big Countries A country is big if it has an area of bigger than 3 million square km or ...

  6. Column Index out of range, 2 > 1 列索引的范围,2 > 1。

    Column Index out of range, 2 > 1  列索引的范围,2 > 1.这个问题是进行数据库查询的时候出现的. 因为查询sql语句时 只查询了 name 然后whil ...

  7. Android 模糊搜索rawquery bind or column index out of range: handle 0x2fb180 报错

    做模糊搜索时,出现了  bind or column index out of range: handle 0x2fb180 报错 public Cursor getTitle(String word ...

  8. ERROR: Functions in index expression must be marked IMMUTABLE

    在创建函数索引时遇到报错,报错信息即为标题,下面是详细信息. 1 表定义 1234567 skytf=> \d test_39; Table "skytf.test_39" ...

  9. MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems

          早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...

  10. '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp error

    '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp error 异常现象 ### Cause: java.sql.SQ ...

随机推荐

  1. ASP.NET Core 中的速率限制中间件的使用

    简介 在ASP.NET Core中,速率限制中间件是用来控制客户端对Web API或MVC应用程序发出请求的速率,以防止服务器过载和提高安全性. 下面是 AddRateLimiter 的一些基本用法: ...

  2. [转]Ceres求解优化问题

    1. 简介Ceres Solver是专门用于求解非线性最小二乘问题的C++开源库,研究SLAM方向不过滤波和优化两个技术路线,因此常用Ceres库解决实际项目中的优化问题,当然还有g2o同样可用,但就 ...

  3. [转]在MyBatis中使用pageHelper5.1.9分页插件实现物理分页

    pagehelper的GIT地址:https://github.com/pagehelper/Mybatis-PageHelper/ 废话少说,直接给出中文官方链接: 1.如何使用分页插件 2.HOW ...

  4. Web端IM系统:RainbowChat-Web v4.0发布

    关于RainbowChat-Web ► RainbowChat-Web详细介绍:http://www.52im.net/thread-2483-1-1.html► 历史版本更新记录:http://ww ...

  5. Android RNDIS gadget Windows免驱修改方案

    过程简单粗暴,拿到竞品的设备,然后使用UsbTreeView查看设备的相关描述符. 对比发现接口抽象描述符和接口描述符不一致,直接修改Linux RNDIS gadget 驱动,将驱动中有关的两个描述 ...

  6. 前端学习openLayers配合vue3(图层中心点的偏移)

    有了上一步的学习,我们知道了如何创建一个地图,现在我们来尝试更改一下图层的中心点 关键代码 let view = map.getView();//获取视图层 let center = view.get ...

  7. 「V 曲收集」幸运

      标题原为"絮语",但终究是别人的絮语.不过是在他们的演唱下,我有这"幸运"拾起它们吧.   啊,还是加一个维护日志好了. [2022/08/31] 开启日志 ...

  8. 开源的分布式事务解决方案-Seata

    Seata 是什么? (1)Seata 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务. (2)在 Seata 开源之前,Seata 对应的内部版本在阿里经济 ...

  9. flink基础教程

    随着大数据技术在各行各业的广泛应用,要求能对海量数据进行实时处理的需求越来越多,同时数据处理的业务逻辑也越来越复杂,传统的批处理方式和早期的流式处理框架也越来越难以在延迟性.吞吐量.容错能力以及使用便 ...

  10. 京东从 OpenStack 改用 Kubernetes 的始末

    构建集群的历史 物理机器的时代(2004年-2014年) 在2014年之前,我们公司的应用程序都部署在物理机器上.在物理机器时代,为了给即将上线的应用程序分配物理机器,我们平均需要等上一周的时间.由于 ...