Having this json:
{
f1: "abc",
f2: [
{id: 1, val:"a"},
{id: 2, val:"b"},
{id: 3, val:"c"}
],
f3: [
"a",
"b",
"c"
]
}
Update:
As an example:
SELECT JSON_SEARCH(
'{"f1": "abc", "f2": [{"id": "1", "val":"a"},{"id": "2", "2":"b"},{"id": 3, "val":"2"}], "f3": ["a","b","c"]}',
'all', '1', null, '$.f2[*].id');
returns the needed path for f2->id==1
I can then use
select json_set(
'{"f1": "abc", "f2": [{"id": "1", "val":"a"},{"id": "2", "2":"b"},{"id": 3, "val":"2"}], "f3": ["a","b","c"]}',
'$.f2[0].val', 'd');
to update the data.
but
SELECT JSON_SEARCH(
'{"f1": "abc", "f2": [{"id": 1, "val":"a"},{"id": "2", "2":"b"},{"id": 3, "val":"2"}], "f3": ["a","b","c"]}',
'all', '1', null, '$.f2[*].id');
won't find f2->id==1 . Neither does
SELECT JSON_SEARCH(
'{"f1": "abc", "f2": [{"id": 1, "val":"a"},{"id": "2", "2":"b"},{"id": 3, "val":"2"}], "f3": ["a","b","c"]}',
'all', 1, null, '$.f2[*].id');
=========
update 2:
I will just save the IDs as string... But right now i have onother problem:
SELECT JSON_SEARCH(
'{"mm": [{"id":"1","field":"test","value":33}]}',
'one', '1', null, '$.mm[*].id') as path;
is working
SELECT * FROM document_data where document_id=5;
update document_data set data=JSON_SET(data, '$.mm', json_array()) where document_id=5;
update document_data set data=JSON_ARRAY_APPEND(data, '$.mm', '{"id":"1","field":"test","value":33}') where document_id=5;
SELECT JSON_SEARCH(data, 'one', '1', null, '$.mm[*].id') as path from document_data where id='5';
is not working. Seems to be the quoting.
Can someone help?
==========
how can i update f2 where id==2 using JSON_SET?
Tried everything but i can't seem to figure out how to do it.
Many thanks
Rene
JSON_SEARCH, by design, seems to search only strings, see WL#7909: Server side JSON functions :: JSON_SEARCH.
One option, very unintuitive is to use something like (be careful with performance problems):
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.20 |
+-----------+
1 row in set (0.00 sec)
mysql> SET #`json` := '{
'> "f1": "abc",
'> "f2": [
'> {"id": 1, "val": "a"},
'> {"id": 2, "val": "b"},
'> {"id": 3, "val": "c"}
'> ],
'> "f3": ["a", "b", "c"]
'> }',
-> #`value` := 2,
-> #`base_path` := '$.f2';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT JSON_SEARCH(
-> REPLACE(
-> REPLACE(
-> REPLACE(
-> JSON_EXTRACT(#`json`, CONCAT(#`base_path`, '[*].id')),
-> ', ', '","'),
-> '[', '["'),
-> ']', '"]'),
-> 'one', #`value`) INTO #`path`;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT #`path`;
+---------+
| #`path` |
+---------+
| "$[1]" |
+---------+
1 row in set (0.00 sec)
mysql> SELECT
-> CONCAT(
-> REPLACE(
-> JSON_UNQUOTE(#`path`),
-> '$',
-> #`base_path`
-> ),
-> '.val') INTO #`path`;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT #`path`;
+-------------+
| #`path` |
+-------------+
| $.f2[1].val |
+-------------+
1 row in set (0.00 sec)
mysql> SELECT JSON_SET(#`json`, #`path`, 'd');
+-------------------------------------------------------------------------------------------------------------------+
| JSON_SET(#`json`, #`path`, 'd') |
+-------------------------------------------------------------------------------------------------------------------+
| {"f1": "abc", "f2": [{"id": 1, "val": "a"}, {"id": 2, "val": "d"}, {"id": 3, "val": "c"}], "f3": ["a", "b", "c"]} |
+-------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
See db-fiddle.
I want to ignore null value from query .
I tried with case statement inside concat but its give me null values
The table structure
CREATE TABLE `UserInfo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`key` varchar(200) NOT NULL,
`value` longtext NOT NULL,
`created_on` varchar(200) NOT NULL,
`updated_on` varchar(200) NOT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=111 DEFAULT CHARSET=latin1;
insert into `UserInfo`(`id`,`user_id`,`key`,`value`,`created_on`,`updated_on`,`status`)
values (69,11132,'country','India','','',1),(67,11132,'city','Gurgaon','','',1),(68,11132,'state','Haryana','','',1),(66,11132,'address','CyberHub','','',1),(64,11131,'state','Delhi','','',1),(65,11131,'country','India','','',1),(63,11131,'city','','','',1),(62,11131,'address','Lajpat Nagar','','',1),(60,11130,'state','Haryana','','',1),(61,11130,'country','India','','',1),(58,11130,'address','','','',1),(59,11130,'city','Gurgaon','','',1),(787,11130,'password','0192023a7bbd73250516f069df18b500','30-03-2016 06:58:18','30-03-2016 06:58:18',1),(788,11131,'password','04d237e63ebc1acbb3fcacf3e1a846cb','30-03-2016 06:58:18','26-04-2016 07:46:33',1),(789,11132,'password','0192023a7bbd73250516f069df18b500','30-03-2016 06:58:18','24-05-2016 11:47:35',1),(1123,11130,'parent','0','','',1),(1124,11131,'parent','0','','',1),(1125,11132,'parent','0','','',1),(2036,11136,'gcm_id','fLJ8vwS5yWc','04-05-2016 06:01:07','04-05-2016 06:01:07',1),(2035,11136,'push_id','fLJ8vwS5yWc:APA91bFO-hLW5uQqDQTABkilyOd9MzuMRhQMI8uNLRGh4fjaq3Bk1OvFmb7QFVKYpqqJZFPQ78Y1h0349IZuxq0EcxZ8VCHJOTOhhsqi1VxQ2A7TVLY-phDcN6sj80x8R7KoOocQKPLl','04-05-2016 06:01:07','04-05-2016 06:01:07',1),(2993,11570,'transaction_limit','100','16-09-2016 10:34:13','16-09-2016 10:34:13',1),(2992,11570,'otp','461178','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2991,11570,'password','1e28284f59e926547bb6793ad8723722','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2990,11570,'imei','353918057482479','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2989,11570,'push_id','dGxCE33MJgg:APA91bFCzSkvIDEcZcgmmSxVocFNZfLI8owLsdElmF-Et0wUH0mxUbQ9mIZDMjlhqClTOYiSxhSaVBdESwJW6J58hsyEF0LUMgXRTGdnEwWdbHJmm3EZuHDHzniMaJGcCKvALrcxtQt8','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2987,11570,'gender','Male','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2026,11136,'gcm_id','fLJ8vwS5yWc','03-05-2016 06:28:52','03-05-2016 06:28:52',1),(2025,11136,'push_id','fLJ8vwS5yWc:APA91bFO-hLW5uQqDQTABkilyOd9MzuMRhQMI8uNLRGh4fjaq3Bk1OvFmb7QFVKYpqqJZFPQ78Y1h0349IZuxq0EcxZ8VCHJOTOhhsqi1VxQ2A7TVLY-phDcN6sj80x8R7KoOocQKPLl','03-05-2016 06:28:52','03-05-2016 06:28:52',1),(2024,11136,'gcm_id','cU6PMImJ9Ms','02-05-2016 17:02:39','02-05-2016 17:02:39',1),(2023,11136,'push_id','cU6PMImJ9Ms:APA91bHA-a_joHtzEBgoAPRxRCXObeJLlCCNSxlCM-jBZKvSUEFvhLZEyqPbbsIDyWc2emZa1yBerOQLGXgzzNKZmcJYCgcF8KBdm9McVBiDKzU_OthjGnROZyTC5EvnI7Z4QivXnokA','02-05-2016 17:02:39','02-05-2016 17:02:39',1),(2022,11136,'gcm_id','cU6PMImJ9Ms','02-05-2016 16:38:55','02-05-2016 16:38:55',1),(2020,11136,'gcm_id','c2sOUeSpIuQ','02-05-2016 16:37:53','02-05-2016 16:37:53',1),(2021,11136,'push_id','cU6PMImJ9Ms:APA91bHA-a_joHtzEBgoAPRxRCXObeJLlCCNSxlCM-jBZKvSUEFvhLZEyqPbbsIDyWc2emZa1yBerOQLGXgzzNKZmcJYCgcF8KBdm9McVBiDKzU_OthjGnROZyTC5EvnI7Z4QivXnokA','02-05-2016 16:38:55','02-05-2016 16:38:55',1),(2019,11136,'push_id','c2sOUeSpIuQ:APA91bGskmvA5VVmxozMHKX3qHc16bdmk9h5gFTEPP8uFUYsO-doGCnkTE-ZtpMGeuuk2YCt3Ja56ey7nIga6aO7wpof2fI5zcgYdFACKvkcddNAlY4UhIO39tfyG3m4DZkmaLqw2Kxu','02-05-2016 16:37:53','02-05-2016 16:37:53',1),(2018,11136,'gcm_id','djRRW-PrS2Q','02-05-2016 16:30:02','02-05-2016 16:30:02',1),(2016,11136,'gcm_id','c2sOUeSpIuQ','02-05-2016 16:25:00','02-05-2016 16:25:00',1),(2017,11136,'push_id','djRRW-PrS2Q:APA91bFbB5SO0wY2TlGCZBmgbZUr0c1FXH5FE22YxOCf4tA7uM1V45T2cTY1aitIQKrf1bjrPB-zZUCDPNdL8-2SYnEawrFTdVjL5w1VuVI2kA89ixlK6jlpmNOoJ7wQnWqQoCFkcd9_','02-05-2016 16:30:02','02-05-2016 16:30:02',1),(2015,11136,'push_id','c2sOUeSpIuQ:APA91bGskmvA5VVmxozMHKX3qHc16bdmk9h5gFTEPP8uFUYsO-doGCnkTE-ZtpMGeuuk2YCt3Ja56ey7nIga6aO7wpof2fI5zcgYdFACKvkcddNAlY4UhIO39tfyG3m4DZkmaLqw2Kxu','02-05-2016 16:25:00','02-05-2016 16:25:00',1),(2014,11136,'gcm_id','djRRW-PrS2Q','01-05-2016 07:38:26','01-05-2016 07:38:26',1),(2013,11136,'push_id','djRRW-PrS2Q:APA91bFbB5SO0wY2TlGCZBmgbZUr0c1FXH5FE22YxOCf4tA7uM1V45T2cTY1aitIQKrf1bjrPB-zZUCDPNdL8-2SYnEawrFTdVjL5w1VuVI2kA89ixlK6jlpmNOoJ7wQnWqQoCFkcd9_','01-05-2016 07:38:26','01-05-2016 07:38:26',1),(2012,11136,'gcm_id','c2sOUeSpIuQ','01-05-2016 07:29:00','01-05-2016 07:29:00',1),(2011,11136,'push_id','c2sOUeSpIuQ:APA91bGskmvA5VVmxozMHKX3qHc16bdmk9h5gFTEPP8uFUYsO-doGCnkTE-ZtpMGeuuk2YCt3Ja56ey7nIga6aO7wpof2fI5zcgYdFACKvkcddNAlY4UhIO39tfyG3m4DZkmaLqw2Kxu','01-05-2016 07:29:00','01-05-2016 07:29:00',1),(2010,11439,'otp','467957','01-05-2016 07:04:42','16-08-2016 10:48:09',0),(2009,11438,'otp','631866','30-04-2016 09:36:54','06-05-2016 13:32:15',0),(2008,11438,'otp','586481','30-04-2016 09:22:55','06-05-2016 13:32:15',0),(2007,11438,'otp','971411','30-04-2016 09:19:52','06-05-2016 13:32:15',0),(2006,11136,'gcm_id','eNJISa-1OGA','30-04-2016 09:05:22','30-04-2016 09:05:22',1),(2005,11136,'push_id','eNJISa-1OGA:APA91bGB6m0d-RzoFUvw1SRsCIOEvfO3BjLUiMsZRdHnHlEGq6QKV9sVqCdLai25_dxTvUeuQVzFU5scKCaPvlDaG_VZ-V51t1AG4EEAtJdjGNQwhITF2eh0E24D_J9vPCQ3nIzanq8f','30-04-2016 09:05:22','30-04-2016 09:05:22',1),(2004,11445,'transaction_limit','2000','30-04-2016 09:03:41','30-04-2016 09:03:41',1),(2003,11445,'otp','230253','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(2002,11445,'password','25d55ad283aa400af464c76d713c07ad','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(2001,11445,'push_id','ccPZg_PD7bo:APA91bFrUK8JPZfI6tSBKDgETeeI_aE6RA-FTFh6pS4fluVv6jHIVKUyDAEurIJWo49nWa52q6Zas7F2DI_KNkDX-HVq_TXuO36dRN9JditQVGR9HUq8avS5QEzYA2gwYjyo7Bosrsyz','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(2000,11445,'gcm_id','ccPZg_PD7bo','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(1999,11445,'gender','Male','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(1998,11445,'bank_name','DCB Bank','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(1997,11136,'gcm_id','cXK3F2L7Fcw','30-04-2016 08:37:01','30-04-2016 08:37:01',1)
I tried with query but its produce null column value as well
I want to ignore null value
SELECT
CONCAT(
CASE WHEN ui.key='gender' THEN ui.value ELSE '' END,
CASE WHEN ui.key='password' THEN ui.value ELSE '' END,
CASE WHEN ui.key='transaction_limit' THEN ui.value ELSE '' END
) AS test,
ui.id,ui.user_id,ui.key,ui.value
FROM UserInfo ui
WHERE ui.user_id=11445
Result :
------------------------------------------------------------------------------------------------------------------
test id user_id key value
2000 2004 11445 transaction_limit 2000
2003 11445 otp 230253
25d55ad 2002 11445 password 25d55ad..
2001 11445 push_id ccPZg_P..
2000 11445 gcm_id ccPZg_PD7bo
Male 1999 11445 gender Male
1998 11445 bank_name DCB Bank
------------------------------------------------------------------------------------------------------------------
Desire Result :
------------------------------------------------------------------------------------------------------------------
test id user_id key value
2000 2004 11445 transaction_limit 2000
25d55ad 2002 11445 password 25d55ad..
Male 1999 11445 gender Male
------------------------------------------------------------------------------------------------------------------
Sorry for the wron answer:
you can add Having:
SELECT
CONCAT(
CASE WHEN ui.key='gender' THEN ui.value ELSE '' END,
CASE WHEN ui.key='password' THEN ui.value ELSE '' END,
CASE WHEN ui.key='transaction_limit' THEN ui.value ELSE '' END
) AS test,
ui.id,ui.user_id,ui.key,ui.value
FROM UserInfo ui
WHERE ui.user_id=11445
HAVING test <> '';
sample
mysql> SELECT
-> CONCAT(
-> CASE WHEN ui.key='gender' THEN ui.value ELSE '' END,
-> CASE WHEN ui.key='password' THEN ui.value ELSE '' END,
-> CASE WHEN ui.key='transaction_limit' THEN ui.value ELSE '' END
-> ) AS test,
-> ui.id,ui.user_id,ui.key,ui.value
-> FROM UserInfo ui
-> WHERE ui.user_id=11445
-> HAVING test <> '';
+----------------------------------+------+---------+-------------------+----------------------------------+
| test | id | user_id | key | value |
+----------------------------------+------+---------+-------------------+----------------------------------+
| 2000 | 2004 | 11445 | transaction_limit | 2000 |
| 25d55ad283aa400af464c76d713c07ad | 2002 | 11445 | password | 25d55ad283aa400af464c76d713c07ad |
| Male | 1999 | 11445 | gender | Male |
+----------------------------------+------+---------+-------------------+----------------------------------+
3 rows in set (0,00 sec)
mysql>
You can use CONCAT_WS instead of CONCAT . The first arg by CONCAT_WS ist the separetor
sample
mysql> select CONCAT('a','b',NULL,'d','e');
+------------------------------+
| CONCAT('a','b',NULL,'d','e') |
+------------------------------+
| NULL |
+------------------------------+
1 row in set (0,01 sec)
mysql> select CONCAT_WS(',','a','b',NULL,'d','e');
+-------------------------------------+
| CONCAT_WS(',','a','b',NULL,'d','e') |
+-------------------------------------+
| a,b,d,e |
+-------------------------------------+
1 row in set (0,00 sec)
mysql>
Manual:
CONCAT_WS() stands for Concatenate With Separator and is a special
form of CONCAT(). The first argument is the separator for the rest of
the arguments. The separator is added between the strings to be
concatenated. The separator can be a string, as can the rest of the
arguments.
If the separator is NULL, the result is NULL; all other NULL values
are skipped. This makes CONCAT_WS() suitable when you want to
concatenate some values and avoid losing all information if one of
them is NULL.