mysql-json

mySQL 5.7 return row as json using new json features

ε祈祈猫儿з 提交于 2019-12-07 04:51:05
问题 I was going over some of the new JSON features and was wondering if there is a clever (or obvious) way to return a rowset as a JSON object. Ideally without having to name the keys or use any kind of string manipulation. Example: TABLE: people id name age 1 bob 54 2 jay 32 3 john 10 SELECT * FROM people where id = 1 would return {"id":1,"name":"bob","age":54} Or even better SELECT * FROM people would return an array of all 3 objects If you are not familiar with the new JSON features, one of

MySQL nested JSON column search and extract sub JSON

回眸只為那壹抹淺笑 提交于 2019-12-06 22:29:15
问题 I have a MySQL table authors with columns id , name and published_books . In this, published_books is a JSON column. With sample data, id | name | published_books ----------------------------------------------------------------------- 1 | Tina | { | | "17e9bf8f": { | | "name": "Book 1", | | "tags": [ | | "self Help", | | "Social" | | ], | | "language": "English", | | "release_date": "2017-05-01" | | }, | | "8e8b2470": { | | "name": "Book 2", | | "tags": [ | | "Inspirational" | | ], | |

Pop the last item in a JSON Array in MySQL 5.7

為{幸葍}努か 提交于 2019-12-06 00:03:02
I've got an array of dates in a field called from . It can look something like this. ['2016-05-01', '2016-05-03', '2016-05-04'] I want to SELECT the last item (here 2016-05-04 ). I've tried this: SELECT `from`->"$[JSON_LENGTH(`from`) - 1]" FROM `table` WHERE `id` = 3; but got that error: ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 2. I've tried using a variable like this : SET @count = (SELECT JSON_LENGTH(`from`) - 1 FROM `table` WHERE `id` = 3); SELECT `from`->"$[@count]" FROM `table` WHERE `id` = 3; but got the exact same error. But if I do:

mySQL 5.7 return row as json using new json features

こ雲淡風輕ζ 提交于 2019-12-05 09:53:37
I was going over some of the new JSON features and was wondering if there is a clever (or obvious) way to return a rowset as a JSON object. Ideally without having to name the keys or use any kind of string manipulation. Example: TABLE: people id name age 1 bob 54 2 jay 32 3 john 10 SELECT * FROM people where id = 1 would return {"id":1,"name":"bob","age":54} Or even better SELECT * FROM people would return an array of all 3 objects If you are not familiar with the new JSON features, one of the new functions is JSON_OBJECT SELECT JSON_OBJECT('key1', 1, 'key2', 'abc') would return a key value

Chaining JSON_EXTRACT with CAST or STR_TO_DATE fails

依然范特西╮ 提交于 2019-12-05 05:53:28
I'm trying to extract a datetime from a JSONFIELD "data" in MySQL. If I do a simple JSON_EXTRACT however, the return field type is a JSON. mysql> select JSON_EXTRACT(data, "$.new_time") from analytics limit 10; +----------------------------------+ | JSON_EXTRACT(data, "$.new_time") | +----------------------------------+ | NULL | | "2016-09-30T04:00:00+00:00" | | "2016-09-29T05:30:00+00:00" | | NULL | | "2016-10-01T05:30:00+00:00" | | "2016-09-27T23:00:00+00:00" | | NULL | | "2016-09-23T01:30:00+00:00" | | "2016-09-23T04:00:00+00:00" | | "2016-09-27T01:30:00+00:00" | +--------------------------

MySQL nested JSON column search and extract sub JSON

两盒软妹~` 提交于 2019-12-05 03:27:20
I have a MySQL table authors with columns id , name and published_books . In this, published_books is a JSON column. With sample data, id | name | published_books ----------------------------------------------------------------------- 1 | Tina | { | | "17e9bf8f": { | | "name": "Book 1", | | "tags": [ | | "self Help", | | "Social" | | ], | | "language": "English", | | "release_date": "2017-05-01" | | }, | | "8e8b2470": { | | "name": "Book 2", | | "tags": [ | | "Inspirational" | | ], | | "language": "English", | | "release_date": "2017-05-01" | | } | | } -----------------------------------------

PDO-MySQL: Boolean values get converted to 1 or empty string on prepared statement binding

若如初见. 提交于 2019-12-02 05:14:21
问题 I'm trying to insert some boolean values into JSON-type columns. $taskSql = "INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', :title, 'done', :done), :taskListId)"; $taskStatement = $connection->prepare($taskSql); $taskStatement->execute([":title" => $task->title, ":done" => $task->done, ":taskListId" => $id]); Which results in following SQL being executed. -- $task->done is false INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', 'New Task', 'done', ''), '12')

PDO-MySQL: Boolean values get converted to 1 or empty string on prepared statement binding

不羁的心 提交于 2019-12-02 01:13:07
I'm trying to insert some boolean values into JSON-type columns. $taskSql = "INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', :title, 'done', :done), :taskListId)"; $taskStatement = $connection->prepare($taskSql); $taskStatement->execute([":title" => $task->title, ":done" => $task->done, ":taskListId" => $id]); Which results in following SQL being executed. -- $task->done is false INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', 'New Task', 'done', ''), '12') -- $task->done is true INSERT INTO Tasks (data, taskListId) VALUES (JSON_OBJECT('title', 'New Task',

PHP PDO query error on table has json data type (MySQL 5.7.8-rc)

送分小仙女□ 提交于 2019-11-30 14:06:13
I'm trying new json datatype on mysql 5.7. When I use native php mysql query, it works fine, but when I use PDO to query data, it shows this error: Error: exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2036 ' in /some_folder/pdo.php:12 Stack trace: #0 /some_folder/pdo.php(12): PDO->query('select * from table_has_json_datatype') #1 {main} Do you guys know how to solve this problem ? Thanks. Update with my simple test code: <?php try{ $db = new PDO('mysql:host=some.host;dbname=somedb;charset=utf8', 'user', 'pwd'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE

MySQL 5.7.12 import cannot create a JSON value from a string with CHARACTER SET 'binary'

我只是一个虾纸丫 提交于 2019-11-28 15:39:47
I exported my database with JSON columns in it. After I migrated to a new server, my import crashed every time with an error like: cannot create a JSON value from a string with CHARACTER SET 'binary' On stackoverflow, I found this post but didn't work for me: mysqlimport issues "set @@character_set_database=binary" which prevents loading json values The file is 2GB and isn't possible to open the file. Anyone has an idea to import my database file? You can apply a regex to the SQL text which you exported which will convert your binary strings into an insertable format. This was my quick and