Coder Social home page Coder Social logo

Comments (6)

methane avatar methane commented on June 27, 2024

when ran directly on the database

How did you run prepared query directly on the database, without sending query from any client?

from mysql.

methane avatar methane commented on June 27, 2024

I assume "directly" means "not using prepared statement", and you used some client like "mysql" command. Please write a "complete step" next time.

Anyway, this is what happend in MySQL.

  • json->'$.alive' : this value doesn't have boolean type, but JSON type.
  • json->'$.alive' == true : MySQL convert "true" to JSON "true" automatically. So it works fine.
  • json->'$.alive' == ? : MySQL doesn't convert the parameter "true". Since JSON true != BOOLEAN true (e.g. TINYINT 1), this is false.

Since TRUE is alias of 1 in MySQL, you need to write this complex code.

	query := "SELECT * FROM test_table WHERE json->'$.alive' = CAST(? AS JSON)"
	rows, err := db.Query(query, "true") // Not true, but "true"!

Full code is here.
https://github.com/methane/sandbox/blob/master/2023/go_mysql_1465/main.go

from mysql.

methane avatar methane commented on June 27, 2024

There may be a better way. But we are expert of mysql client/server protocol, not query.
Ask support on MySQL community, not on here.

from mysql.

methane avatar methane commented on June 27, 2024

Maybe, this is easier.

       query := "SELECT * FROM test_table WHERE CAST(json->'$.alive' AS UNSIGNED) = ?"
       rows, err := db.Query(query, true)

from mysql.

boyer-victor avatar boyer-victor commented on June 27, 2024

I misspoke. I was issuing to the database via my mysql driver in VSCode. I just tried (in a fresh docker image for sanity) as a prepared statement ie:

PREPARE stmt FROM "SELECT * FROM test_table WHERE json->'$.alive' = ?";
SET @a = true;
EXECUTE stmt USING @a;

Hmm. Interesting though, I was aware of it being stored as a TINYINT(1), but didn't realize that there was some additional type handling that went on with prepared statements here. Thank you for the help, I will try that. (and hopefully not have to have where clauses on json fields anyways for production filtering...)

from mysql.

methane avatar methane commented on June 27, 2024
PREPARE stmt FROM "SELECT * FROM test_table WHERE json->'$.alive' = ?";
SET @a = true;
EXECUTE stmt USING @a;

I execute it, but result is "0 rows in set (0.00 sec)".
It is same to this driver.

from mysql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.