Reputation: 17
I'm using MySQL v5.7.42 and Go v1.21.0 with the volatiletech/sqlboiler v3.7.1 library. I have a table with a JSON column, and I'm trying to sum values extracted from this JSON using JSON_EXTRACT().
Here's a simplified version of my setup:
CREATE TABLE users {
name json NOT NULL
}
mysql> select name from users;
+--------------------------------------------------------+
| name |
+--------------------------------------------------------+
| {"hoge1": {"fuga1": 100000}} |
| {"hoge2": {"fuga2": 1000000}} |
+--------------------------------------------------------+
In Go, I'm executing the following SQL queries and scanning the results into int64 variables:
import (
...
"github.com/volatiletech/sqlboiler/queries"
"database/sql"
)
...
var (
v1 v2 int64
db *sql.DB
)
...
row1 := queries.Raw(`
select sum(JSON_EXTRACT(name, '$.hoge1.fuga1')) as piyo from users;
`).QueryRow(db)
err := row1.Scan(&v1)
fmt.Printf("%v, err\n", v1, err) // output: 100000, nil
row2 := queries.Raw(`
select sum(JSON_EXTRACT(name, '$.hoge2.fuga2')) as piyo from users;
`).QueryRow(db)
err = row2.Scan(&v2)
fmt.Printf("%v, err\n", v2, err) // output: 0, Scan error on column index 0, name "piyo": converting driver.Value type float64 ("1e+06") to a int64: invalid syntax
The first query works as expected, but the second query returns the following error:
Scan error on column index 0, name "piyo": converting driver.Value type float64 ("1e+06") to a int64: invalid syntax
My Questions:
Context:
What is the return type of SUM() in mysql?
The existing above question discusses the return type of SUM() but does not address issues with JSON_EXTRACT() specifically or how it interacts with different numeric types in the context of JSON data. My issue involves both SUM() and JSON_EXTRACT() which is not covered in the referenced question.
Upvotes: 0
Views: 37