mysql-json

mysqlimport issues “set @@character_set_database=binary” which prevents loading json values

落爺英雄遲暮 提交于 2019-12-23 09:54:45
问题 I have been using mysqlimport without problems for a long time, now as mysql 5.7 added json data type support, I'm trying to use mysqlimport with rows containing json data. Here is an example of a row in csv file that will be imported using mysqlimport: column_A_value,column_B_value,[{"x":20,"y":"some name"}] Notice that the last column type is json. Now when using mysqlimport as the following: mysqlimport -u user -ppass -h localhost --columns='col_A,col_B,col_C' --local --fields-terminated

MySQL JSON: How to find object from key value

末鹿安然 提交于 2019-12-20 04:09:30
问题 I'm trying to find a way to search a JSON object and get a particular key but search on another key. Here is an example schema: CREATE TABLE `fields` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `label` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, `options` json DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; INSERT INTO `fields` (label, options) VALUES ( 'My Field', '[{"text": "Grass", "value": "1"}, {"text": "Synthetic (New Type -

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

亡梦爱人 提交于 2019-12-18 16:45:07
问题 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

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

試著忘記壹切 提交于 2019-12-17 17:25:51
问题 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? 回答1: You can apply a regex to the SQL text

Using MySQL JSON field to join on a table

亡梦爱人 提交于 2019-12-17 11:09:51
问题 I have a json field that stores a list of ids (not best practice here I know), I want to know if it's possible to use do operations on this JSON field and use them in the sql. Below is a fictitious example of what I'm trying to achieve, is something like this doable? CREATE TABLE user ( user_id INT, user_name VARCHAR(50), user_groups JSON ); CREATE TABLE user_group ( user_group_id INT, group_name VARCHAR(50) ); INSERT INTO user_group (user_group_id, group_name) VALUES (1, 'Group A'); INSERT

Using MySQL JSON field to join on a table

守給你的承諾、 提交于 2019-12-17 11:09:11
问题 I have a json field that stores a list of ids (not best practice here I know), I want to know if it's possible to use do operations on this JSON field and use them in the sql. Below is a fictitious example of what I'm trying to achieve, is something like this doable? CREATE TABLE user ( user_id INT, user_name VARCHAR(50), user_groups JSON ); CREATE TABLE user_group ( user_group_id INT, group_name VARCHAR(50) ); INSERT INTO user_group (user_group_id, group_name) VALUES (1, 'Group A'); INSERT

How to search JSON array in MySQL?

拈花ヽ惹草 提交于 2019-12-17 10:48:07
问题 Let's say I have a JSON column named data in some MySQL table, and this column is a single array . So, for example, data may contain: [1,2,3,4,5] Now I want to select all rows which have a data column where one of its array elements is greater than 2. Is this possible? I tried the following, but seems it is always true regardless of the values in the array: SELECT * from my_table WHERE JSON_EXTRACT(data, '$[*]') > 2; 回答1: SELECT JSON_SEARCH('["1","2","3","4","5"]', 'one', "2") is not null is

JSON vs. Serialized Array in database [closed]

若如初见. 提交于 2019-12-17 10:11:19
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . What are the advantages and disadvantages of storing JSON data in MySQL database vs. serialized array? 回答1: JSON encode() & decode() PHP Version >= 5.0.0 Nesting Limit of 20. PHP Version >= 5.2.3 Nesting Limit of 128. PHP Version >= 5.3.0 Nesting Limit of 512. Small footprint

Chaining JSON_EXTRACT with CAST or STR_TO_DATE fails

浪尽此生 提交于 2019-12-10 03:42:02
问题 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

MySQL JSON - using IN statement | json_contains

人盡茶涼 提交于 2019-12-08 01:42:40
问题 I'm trying to select all columns where the roles property in the json column contains ANY of the values. Statements I've tried: SELECT * FROM components WHERE json->'$.roles' IN(1) this doesn't even work but it should in my opinion... SELECT * FROM components WHERE JSON_CONTAINS(components, '1', '$.roles') this does work however is strict, so when I use 1 it pulls both like it should because they both contain 1, however if I insert 1,2 or JSON_ARRAY(1,2) it will only pull the later row