前言

使用 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. Socat 命令总结

    事以密成,语以泄败. 导航 介绍 基本语法 用法示例 回显输入 回显输入 over TCP/UDP 正向连接 shell 反向连接 shell 端口转发 网络服务 文件传输 管道传输 加密传输 TUN ...

  2. Iframe标签显示目标网页的指定区域,视频可全屏可缩小

    由于播放的直播视频有多余的logo和聊天框等所以需要去掉,用Iframe标签显示目标网页的指定区域,视频可全屏可缩小 用自己私人的服务器来测试吧,99买阿里云 HTML代码 <html> ...

  3. 智谱开源CogAgent的最新模型CogAgent-9B-20241220,全面领先所有开闭源GUI Agent模型

    在现代数字世界中,图形用户界面(GUI)是人机交互的核心.然而,尽管大型语言模型(LLM)如ChatGPT在处理文本任务上表现出色,但在理解和操作GUI方面仍面临挑战,因此最近一年来,在学界和大模型社 ...

  4. 总是被低估,从未被超越,揭秘QQ极致丝滑背后的硬核IM技术优化

    本文由腾讯云开发者张曌.毕磊分享,原题"QQ 9"傻快傻快"的?!带你看看背后的技术秘密",本文进行了排版和内容优化等. 1.引言 最新发布的 QQ 9 自上线 ...

  5. 编译Ubuntu 24.04 LTS 内核(BuildYourOwnKernel)

    1.配置环境 修改apt源 修改 /etc/apt/sources.list.d/ubuntu.sources ,添加 "deb-src"到 Types:,修改后的文件内容如下: ...

  6. Linux 压缩命令集合

    01. tar格式 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!) 02. gz格式 解压1:gunzi ...

  7. Solution -「AGC 020F」Arcs on a Circle

    \(\mathscr{Description}\)   Link.   在一个周长为 \(c\) 的圆周上放置长度分别为 \(l_1,l_2,\cdots,l_n\) 的弧,每条弧的位置独立均匀随机. ...

  8. Shenzi pg walkthrough Intermediate window

    nmap └─# nmap -p- -A -sS -T5 192.168.218.55 Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-12-16 ...

  9. Janus Pro:DeepSeek 开源革新,多模态 AI 的未来

    Janus Pro 是 DeepSeek 开发的一个开源多模态人工智能框架,它通过集成视觉和语言处理能力,提供了高性能的多模态任务处理能力. 在线体验: https://deepseek-janusp ...

  10. FLink自定义Sink,生产的数据导出到mysql

    一.自定义生产数据 https://www.cnblogs.com/robots2/p/16048729.html 二.生产转化数据,导出到mysql 2.1 建表语句 CREATE TABLE `v ...