Skip to content

Instantly share code, notes, and snippets.

@mumbleskates
Created November 28, 2023 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mumbleskates/0430b1c7fb87f5a94b69e6191a57a5bb to your computer and use it in GitHub Desktop.
Save mumbleskates/0430b1c7fb87f5a94b69e6191a57a5bb to your computer and use it in GitHub Desktop.
mysql 8 prepared statement bug
mysql> create table t (
-> id integer primary key,
-> val json
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> insert into t(id, val) values (1, '{"a": 1}'), (2, '{"a": "two"}');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> prepare stmt from 'select id from t where json_extract(val, ''$."a"'') = ?';
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> set @a = 'two';
Query OK, 0 rows affected (0.00 sec)
mysql> execute stmt using @a;
+----+
| id |
+----+
| 2 |
+----+
1 row in set (0.00 sec)
mysql> set @a = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> execute stmt using @a;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> set @a = 'two';
Query OK, 0 rows affected (0.00 sec)
mysql> execute stmt using @a;
+----+
| id |
+----+
| 2 |
+----+
1 row in set (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.44 |
+-----------+
1 row in set (0.00 sec)
mysql>
mysql> create table t (
-> id integer primary key,
-> val json
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t(id, val) values (1, '{"a": 1}'), (2, '{"a": "two"}');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> prepare stmt from 'select id from t where json_extract(val, ''$."a"'') = ?';
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> set @a = 'two';
Query OK, 0 rows affected (0.00 sec)
mysql> execute stmt using @a;
+----+
| id |
+----+
| 2 |
+----+
1 row in set (0.00 sec)
mysql> set @a = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> execute stmt using @a;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> set @a = 'two';
Query OK, 0 rows affected (0.00 sec)
mysql> execute stmt using @a;
Empty set, 2 warnings (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.2.0 |
+-----------+
1 row in set (0.00 sec)
mysql>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment