Extracts the value of the corresponding KEY from the string of an array over serialize.

Source: Internet
Author: User
Sometimes a php array variable string that serizlie () has been stored in the data table can be used to retrieve the value of a key in SQL. For example, a: 3: {s: 1: a; s: 12: asdfasdfasdf; s: 1: B; I: 123123123; s: 1: c; s: 8: ASDFASDF;} the key is B. MySQLdelimiter $ createfuncti

Sometimes a php array variable string that serizlie () has been stored in the data table can be used to retrieve the value of a key in SQL. For example, a: 3: {s: 1: a; s: 12: asdfasdfasdf; s: 1: B; I: 123123123; s: 1: c; s: 8: ASDFASDF;} the key is B. MySQL delimiter $ create functi

Sometimes a php array variable string that serizlie () has been stored in the data table can be used to retrieve the value of a key in SQL. For example:
A: 3: {s: 1: "a"; s: 12: "asdfasdfasdf"; s: 1: "B"; I: 123123123; s: 1: "c"; s: 8: "ASDFASDF";} the key is B. MySQL
delimiter $$create function get_from_serialized_json ( in_string varchar(255),   in_key varchar(255) )  returns varchar(255)BEGINreturn trim( '"' from    substring_index(    substring_index(        substring(trim("}" FROM in_string ), locate(concat("\"",in_key,"\""), in_string  )+1     ) , ";",2    ), ":",-1 )) ;END$$
mysql> show create function get_from_serialized_json\G*************************** 1. row ***************************            Function: get_from_serialized_json            sql_mode:      Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `get_from_serialized_json`( in_string varchar(255),   in_key varchar(255) ) RETURNS varchar(255) CHARSET latin1BEGINreturn trim( '"' from    substring_index(    substring_index(        substring(trim("}" FROM in_string ), locate(concat("\"",in_key,"\""), in_string  )+1     ) , ";",2    ), ":",-1 )) ;ENDcharacter_set_client: latin1collation_connection: latin1_swedish_ci  Database Collation: latin1_swedish_ci1 row in set (0.00 sec)
  'asdfasdfasdf', 'b'=> 123123123, 'c'=> 'ASDFASDF');echo serialize($a);
mysql> set @a ='a:3:{s:1:"a";s:12:"asdfasdfasdf";s:1:"b";i:123123123;s:1:"c";s:8:"ASDFASDF";}';Query OK, 0 rows affected (0.00 sec)mysql> mysql> select get_from_serialized_json(@a,"a");+----------------------------------+| get_from_serialized_json(@a,"a") |+----------------------------------+| asdfasdfasdf                     |+----------------------------------+1 row in set (0.00 sec)mysql> select get_from_serialized_json(@a,"b");+----------------------------------+| get_from_serialized_json(@a,"b") |+----------------------------------+| 123123123                        |+----------------------------------+1 row in set (0.00 sec)mysql> select get_from_serialized_json(@a,"c");+----------------------------------+| get_from_serialized_json(@a,"c") |+----------------------------------+| ASDFASDF                         |+----------------------------------+1 row in set (0.00 sec)mysql> mysql> drop function get_from_serialized_json ;Query OK, 0 rows affected (0.00 sec)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.