MySQL JSON_SEARCH - not working with double quotes - mysql

I have json data as follow in user_details:
"[{"value":"sachin","label":"What's your "first" name?"},{"value":"test#example.com","label":"What's your email?"},{"value":"+911234567890","label":"What's your "phone" number?"},{"value":"xyz","label":"What's your city?"},{"value":"abc","label":"What's your address?"}]"
For this I tried with the below query, but it is giving the error. The error happens ONLY when my data contains double quotes ("). How can I make this work?
SELECT CASE WHEN json_search(user_details, 'one', '%name%', null, '$[*].label')
IS NOT NULL THEN 'name'
WHEN json_search(user_details, 'one', '%email%', null, '$[*].label')
IS NOT NULL THEN 'email'
WHEN json_search(user_details, 'one', '%phone number%', null, '$[*].label') IS NOT NULL THEN 'phone'
ELSE 'id' END type,
CASE WHEN json_search(user_details, 'one', '%name%', null, '$[*].label')
IS NOT NULL THEN
json_unquote(json_extract(user_details, concat(json_unquote(replace(json_search(user_details, 'one', '%name%', null,
'$[*].label'),'.label', '')),'.value')))
WHEN json_search(user_details, 'one', '%email%', null, '$[*].label') IS NOT NULL THEN
json_unquote(json_extract(user_details, concat(json_unquote(replace(json_search(user_details, 'one', '%email%', null,
'$[*].label'),'.label', '')),'.value')))
WHEN json_search(user_details, 'one', '%phone number%', null, '$[*].label') IS NOT NULL THEN
json_unquote(json_extract(user_details,concat(json_unquote(replace(json_search(user_details, 'one', '%phone%', null,
'$[*].label'),'.label', '')),'.value')))ELSE user_id END value FROM json_user;

Try:
mysql> SET #`user_details` := '[
'> {"value":"sachin","label":"What\'s your \\"first\\" name?"},
'> {"value":"test#example.com","label":"What\'s your email?"},
'> {"value":"+911234567890","label":"What\'s your \\"phone\\" number?"},
'> {"value":"xyz","label":"What\'s your city?"},
'> {"value":"abc","label":"What\'s your address?"}
'> ]';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT JSON_VALID(#`user_details`);
+-----------------------------+
| JSON_VALID(#`user_details`) |
+-----------------------------+
| 1 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> SELECT
-> JSON_UNQUOTE(
-> JSON_EXTRACT(
-> #`user_details`,
-> JSON_UNQUOTE(
-> REPLACE(
-> JSON_SEARCH(
-> #`user_details`,
-> 'one',
-> '%phone%',
-> null,
-> '$[*].label'
-> ),
-> '.label',
-> '.value'
-> )
-> )
-> )
-> ) `phone`;
+---------------+
| phone |
+---------------+
| +911234567890 |
+---------------+
1 row in set (0.00 sec)

Related

Mysql JSON_REMOVE Array Key and Value (MariaDB)

I have the following JSON in MariaDB/MySQL:
[{"uid": 5}, {"uid": 6}, {"uid": 7}]
user_pst_tb
------------------------------
pst_id | pst_liked_by
--------------------------
1 |[]
-------|----------------
. |[{"uid": 9}]
-------|----------------
. |[]
-------|----------------
29 |[]
-------|----------------
30 | [{"uid": 5}, {"uid": 6}, {"uid": 7}]
i want to use JSON_REMOVE or any method to remove {"uid": 6} alone on pst_id = 30, but i cannot find how to formulate the path. I thought of this:
UPDATE user_pst_tb
SET `pst_liked_by` = JSON_REMOVE(
`pst_liked_by`, JSON_UNQUOTE(
REPLACE(
JSON_SEARCH( `pst_liked_by`, 'one', '6', null, '$**.uid' )
, '.uid'
, ''
)
)
) WHERE pst_id = 30;
for some reason the MariaDB and MySQL docs does not have such examples. Any help is appreciated.
I have also tried:
UPDATE user_pst_tb SET `pst_liked_by`= JSON_REMOVE(`pst_liked_by`, JSON_UNQUOTE( JSON_SEARCH(`pst_liked_by`, 'one','{"uid": 6}') )) WHERE `pst_id` = 30;
The second query clears all the JSON data sadly
UPDATE 1 (some GOOD NEWS)
I have tried this
UPDATE user_pst_tb SET `pst_liked_by` =
JSON_REMOVE(`pst_liked_by`,JSON_UNQUOTE(JSON_search(`pst_liked_by`,
'one', '6'))) WHERE `pst_id` = 30;
Somehow working but it leaves some empty {} behind.
Example: [{"uid": 5}, {}, {"uid": 7}] any idea to remove the empty brackets will be great!!
I was assisted by #ypercubeᵀᴹ
The final query that worked is:
UPDATE nz_psts_01
SET `pst_liked_by` = JSON_REMOVE(
`pst_liked_by`, JSON_UNQUOTE(
REPLACE(
JSON_SEARCH( `pst_liked_by`, 'one', '6', null, '$**.uid' )
, '.uid'
, ''
)
)
) WHERE pst_id = 29
and JSON_SEARCH( `pst_liked_by`, 'one', '6', null, '$**.uid' ) is not null ;
Hope it can help someone

MySQL select last key with value JSON

I have JSON like:
{"1": "6", "2": "10", "3": "12"}
And i would like to get LAST key and value using MySQL query to get output like:
3x12
3 is the last key, and 12 is the last key value...
Is there any MySQL query to do that? I know using reading whole MySQL filed value as posted above and then while loop and if key is last print its value and key...but if is possible in MySQL query to get this output?
I im using this php that reads MySQL field value and get last key and value...but i don't know how to do it in mysql:
$json = json_decode('{"1": "6", "2": "10", "3": "12"}', true);
$value = end($json);
$key = key($json);
echo 'KEY: '.$key.'...VALUE: '.$value;
You can try something like the following, adjust as necessary:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.20 |
+-----------+
1 row in set (0.00 sec)
mysql> SET #`json` := '
'> {
'> "1": "6",
'> "2": "10",
'> "3": "12"
'> }
'> ';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT
-> CONCAT(
-> JSON_UNQUOTE(#`key`),
-> 'x',
-> JSON_UNQUOTE(
-> JSON_EXTRACT(#`json`,
-> CONCAT('$.', #`key`)
-> )
-> )
-> ) `value`
-> FROM (
-> SELECT #`key` := JSON_EXTRACT(
-> JSON_KEYS(#`json`),
-> CONCAT('$[', JSON_LENGTH(#`json`) - 1, ']')
-> )
-> ) `init`;
+-------+
| value |
+-------+
| 3x12 |
+-------+
1 row in set (0.00 sec)
See db-fiddle.

Updateing mysql json field in array

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.

MySQL json_search on numeric values

I've got a json list of objects like that
[{
"something": "bla",
"id": 2
}, {
"something": "yes",
"id": 1
}]
My id field is always a numeric value. But when I try to find id = 2, MySQL returns NULL
select
json_search(
json_extract(
'[{"something": "bla" ,"id": 2}, {"something": "yes","id": 1}]',
"$[*].id"
),
'one',
2
) as json_search;
json_search |
------------|
|
When I use a string as value in my json id object instead of a numeric value, I got a result with Index 0.
select
json_search(
json_extract(
'[{"something": "bla" ,"id": "2"}, {"something": "yes","id": 1}]',
"$[*].id"
),
'one',
"2"
) as json_search;
json_search |
------------|
"$[0]" |
I'm using MySQL 5.7.17
##version |
-----------|
5.7.17-log |
Is numeric search in json arrays not provided in MySQL?
You can try something complicated, not intuitive and possibly with performance problems, but it's an option:
mysql> SELECT JSON_SEARCH(
-> REPLACE(
-> REPLACE(
-> REPLACE(
-> JSON_EXTRACT('[
'> {"something": "bla" ,"id": 2},
'> {"something": "yes","id": 1}
'> ]', "$[*].id"),
-> ', ', '","'),
-> '[', '["'),
-> ']', '"]'),
-> 'one', '2') `json_search`;
+-------------+
| json_search |
+-------------+
| "$[0]" |
+-------------+
1 row in set (0.00 sec)
Although the JSON_EXTRACT function was returning [2, 1] and it was a valid JSON, if you search the documentation the JSON_SEARCH function is:
JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path] ...])
So, as I understood you can only work with STRING values, and not with numeric values. But one solution to your problem could be to use the JSON_CONTAINS function as it returns 1 or 0 if the numeric value exists or not.
select
json_contains(
json_extract(
'[{"something": "bla" ,"id": 2}, {"something": "yes","id": 1}]',
"$[*].id"
),
"2"
) as json_contains;
The only problem is that you could not get the path to the given value within the JSON document. Hope it helped.

How to remove null values in query using CONCAT with CASE statement

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.