我有以下查询,它在直接从 mysql 运行时返回结果。从 golang 程序运行时,相同的查询返回 0 值。package mainimport ( "github.com/rs/zerolog/log" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx")var DB *sqlx.DBfunc main() { DB, err := sqlx.Connect("mysql", "root:password@(localhost:3306)/jsl2") if err != nil { log.Error().Err(err) } sqlstring := `SELECT salesdetails.taxper, sum(salesdetails.gvalue), sum(salesdetails.taxamt) FROM salesdetails Inner Join sales ON sales.saleskey = salesdetails.saleskey where sales.bdate >= '2021-12-01' and sales.bdate <= '2021-12-31' and sales.achead IN (401975) group by salesdetails.taxper order by salesdetails.taxper` rows, err := DB.Query(sqlstring) for rows.Next() { var taxper int var taxableValue float64 var taxAmount float64 err = rows.Scan(&taxper, &taxableValue, &taxAmount) log.Print(taxper, taxableValue, taxAmount) } err = rows.Err() if err != nil { log.Error().Err(err) }}在控制台上,运行程序会返回以下值。在 SQL 浏览器中,它返回 4 行,这是正确的。sql 浏览器对同一查询的结果是0 1278.00 0.005 89875.65 4493.7812 3680.00 441.6018 94868.73 17076.37但在程序中也返回 0 值的 4 行。{"level":"debug","time":"2022-01-13T17:07:39+05:30","message":"0 0 0"}{"level":"debug","time":"2022-01-13T17:07:39+05:30","message":"0 0 0"}{"level":"debug","time":"2022-01-13T17:07:39+05:30","message":"0 0 0"}{"level":"debug","time":"2022-01-13T17:07:39+05:30","message":"0 0 0"}如何设置聚合函数的数据类型。
1 回答
翻翻过去那场雪
TA贡献2065条经验 获得超13个赞
我将 taxper 的数据类型更改为 float 并且它起作用了。我在检查 rows.Scan( 正如@mkopriva 所建议的那样检查错误后发现了这个
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报
0/150
提交
取消