How to remove null values in query using CONCAT with CASE statement - mysql
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.
Related
How to convert tinyint back to boolean when reading from SQL?
`mysql> select * from movies; +----------+-------+---------+ | movie_id | title | watched | +----------+-------+---------+ | 1 | bo | 0 | | 2 | NEW | 0 | | 3 | NEW 2 | 0 | +----------+-------+---------+ CREATE TABLE MOVIES ( movie_id INTEGER NOT NULL AUTO_INCREMENT, title VARCHAR(50) NOT NULL, watched BOOLEAN NOT NULL, PRIMARY KEY (movie_id) ); ` I am having to store the "watched" field as a tiny int instead of typical boolean, I am trying to find a way of converting it back to boolean when reading from table, so I dont have to loop through all responses and convert manually. ie. {movie_id: 1, title: 'bo', watched: 0} ---> {movie_id: 1, title: 'bo', watched: false} I have tried select cast but am unfamiliar with the syntax
MySQL saves Boolean as 0 and 1 as it handles all Boolean that way. It is very practical, then you can add true or false from a comparison in a SUM without CASE WHEN or a FILTER You need still to make a condition to give bak True or False, but they only text of course SELECT movie_id , title , CASE WHEN watched = 0 THEN 'False' ELSE 'True' END IF
This is similar to 'IF' in 'SELECT' statement - choose output value based on column values Borrowing from the answer there, SELECT movie_id, IF (watched > 0, true, false) as bwatched, ... Note that this assumes your schema still includes "NOT NULL" for watched. Without that (or some extra code) NULL would become false. The way "IF()" works is IF(expression , value / expression if true, v /e if false)
Mysql Data truncated for column error when modify a column definition
I have a table with the following column definition `numericValue1` double GENERATED ALWAYS AS ((case when (json_type(json_extract(`object`,'$.numericValue1')) = 'NULL')) then NULL else json_unquote(json_extract(`object`,'$.numericValue1')) end)) VIRTUAL; Now I am trying to run the following script to change the column definition ALTER TABLE `MyTable` MODIFY `numericValue1` double GENERATED ALWAYS AS ((case when (json_type(json_extract(`object`,'$.numericValue1')) = 'NULL') OR (json_type(json_extract(`object`,'$.numericValue1')) = 'STRING') then NULL else json_unquote(json_extract(`object`,'$.numericValue1')) end)) VIRTUAL; I then get an error Data truncated for column 'numericValue1' at row 2 Any ideas? How can I by pass this error?
CREATE TABLE `MyTable`( `object` JSON, `numericValue1` double GENERATED ALWAYS AS (case when (json_type(json_extract(`object`,'$.numericValue1')) = 'NULL') then NULL else json_unquote(json_extract(`object`,'$.numericValue1')) end) VIRTUAL ); INSERT INTO `MyTable` (`object`) VALUES ('{"id":1,"numericValue1":123.456}'), ('{"id":2,"numericValue1":null}'); INSERT INTO `MyTable` (`object`) VALUES ('{"id":1,"numericValue1":"asdf"}'); Data truncated for column 'numericValue1' at row 1 SELECT * FROM `MyTable`; object | numericValue1 :---------------------------------- | ------------: {"id": 1, "numericValue1": 123.456} | 123.456 {"id": 2, "numericValue1": null} | null ALTER TABLE `MyTable` MODIFY `numericValue1` double GENERATED ALWAYS AS ((case when (json_type(json_extract(`object`,'$.numericValue1')) = 'NULL') OR (json_type(json_extract(`object`,'$.numericValue1')) = 'STRING') then NULL else json_unquote(json_extract(`object`,'$.numericValue1')) end)) VIRTUAL; INSERT INTO `MyTable` (`object`) VALUES ('{"id":1,"numericValue1":"asdf"}'); SELECT * FROM `MyTable`; object | numericValue1 :---------------------------------- | ------------: {"id": 1, "numericValue1": 123.456} | 123.456 {"id": 2, "numericValue1": null} | null {"id": 1, "numericValue1": "asdf"} | null db<>fiddle here As you can see only when generated value does not match the datatype then shown error message is produced.
SQL search multitable and contains
Suppose that i have the following data: CREATE TABLE IF NOT EXISTS `Class` ( `id` INT NOT NULL AUTO_INCREMENT, `class name` CHAR(55) NOT NULL, PRIMARY KEY (`id`) ); REPLACE INTO `Class` (`id`,`class name`) VALUES (1,'Mammalia'),(2, 'Amphibia'),(3, 'Aves'),(4, 'Reptile'); CREATE TABLE IF NOT EXISTS `Animals` ( `id` INT NOT NULL AUTO_INCREMENT, `class_id` INT DEFAULT NULL, `name` CHAR(55) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`class_id`) REFERENCES `Class`(`id`) ); INSERT INTO `Animals` (`class_id`,`name`) VALUES (1,'Przewalskis horse'), (1,'Bos taurus'), (1,'Sus scrofa'), (1,'Panthera leo'), (1,'Felis catus'), (1,'Canis lupus'), (1,'Pan troglodytes'), (1,'Nasalis larvatus'), (1,'Ailuropoda melanoleuca'), (1,'Elephas maximus sumatranus'), (1,'Panthera pardus orientalis'), (2, 'Bufu Bufu'), (2, 'Notophthalmus viridescens'), (2, 'Dermophis mexicanus'), (3, 'Campephilus melanoleucos melanoleucos'), (3, 'Tyto alba'), (3, 'Cathartes aura'), (3, 'Serinus canaria'), (3, 'Amazona aestiva'), (3, 'Amazona Oratrix'), (3, 'Anodorhynchus hyacinthinus'), (3, 'Ara ararauna'), (3, 'Ara chloropterus'), (3, 'Strigops habroptilus'), (4, 'Ameivula venetacaudus'), (4, 'Chelonia mydas'), (4, 'Caretta caretta'); I want to do a query with only one input html field that return the following results: if i type: "Amphibia" returns: ------------------- | Class | Animals | ------------------- | Amphibia | Bufu Bufu | | Amphibia | Notophthalmus viridescens | | Amphibia | Dermophis mexicanus | ---------------------------------- if i type: "Amphibia Amazona" returns: ------------------- | Class | Animals | ------------------- | Amphibia | Bufu Bufu | | Amphibia | Notophthalmus viridescens | | Amphibia | Dermophis mexicanus | | Aves | Amazona aestiva | | Aves | Amazona Oratrix | -------------------------- if i type: "caudu" returns ------------------- | Class | Animals | ------------------- | Reptile | Ameivula venetacaudus | -------------------------- Sample codes in Fiddle are welcome! Thankful right now
Considering that input_var is the variable that you will receive from your HTML, you can do the following query: SELECT `class name`, name FROM Animals LEFT JOIN Class ON (Class.id= Animals.class_id) WHERE `class name` like concat('%',SUBSTRING_INDEX(SUBSTRING_INDEX(input_var, ' ', 1), ' ', -1),'%') OR name like concat('%',SUBSTRING_INDEX(SUBSTRING_INDEX(input_var, ' ', 1), ' ', -1),'%') OR `class name` like concat('%',SUBSTRING_INDEX(SUBSTRING_INDEX(input_var, ' ', 2), ' ', -1),'%') OR name like concat('%',SUBSTRING_INDEX(SUBSTRING_INDEX(input_var, ' ', 2), ' ', -1),'%') ; I am also considering that you can receive an input with the inversed animal/class order, like "Amazona Amphibia" instead of "Amphibia Amazona". If may also consider if case sensitive is important to you. If not, you may use upper in both your input string and the table column.
Keep this as your base. select id, `class name`, `name` from animals left join class on (class.id= animals.class_id) And for searching select `class name`, `name` from (select ...) where concat("%", "$search1", "%") like `class name` or concat("%", "$search2", "%") like `name`; To split the first word (mysql) substring_index(substring_index(var, ' ', 1), ' ', -1) and for second word (mysql) substring_index(substring_index(var, ' ', 2), ' ', -1) In PHP you can use substr function.
How to add a character to specific index in string
I have a db with email-addresses which are 15 characters in total. Some are 14 characters long and they have the same in common, they miss a 0 in their name at the 3th index. I had a what similar problem with the id numbers but that was easy to fix because i had to push 0 to the first index. My expected results should be changing 'aa-001#test.me' to 'aa-0001#test.me'.
To add a 0 as 4th character in emails having 14 characters, you may simply use SUBSTR : SELECT CONCAT(SUBSTR(email, 1, 3), '0', SUBSTR(email, 4)) FROM mytable WHERE LENGTH(email) = 14 Another solution is to use REGEXP_REPLACE, with a regular expression that matches only on emails with 14 characters ; this avoids the need for a WHERE clause, as emails that do not match the pattern will be returned untouched : SELECT REGEXP_REPLACE(email, '^(.{3})(.{11})$', CONCAT('$1', '0', '$2')) FROM t db<>fiddle here : WITH t AS (SELECT 'aa-001#test.me' email UNION SELECT 'bb-0002#test.me') SELECT CONCAT(SUBSTR(email, 1, 3), '0', SUBSTR(email, 4)) FROM t WHERE LENGTH(email) = 14 | CONCAT(SUBSTR(email, 1, 3), '0', SUBSTR(email, 4)) | | :----------------------------------------------------- | | aa-0001#test.me | WITH t AS (SELECT 'aa-001#test.me' email UNION SELECT 'bb-0002#test.me') SELECT REGEXP_REPLACE(email, '^(.{3})(.{11})$', CONCAT('$1', '0', '$2')) FROM t | REGEXP_REPLACE(email, '^(.{3})(.{11})$', CONCAT('$1', '0', '$2')) | | :---------------------------------------------------------------- | | aa-0001#test.me | | bb-0002#test.me
MySQL JSON_SEARCH - not working with double quotes
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)