Unserialize through query at database level itself

后端 未结 8 558
清酒与你
清酒与你 2020-12-02 12:53

I have a column value stored in the database as:

a:2:{i:0;s:2:\"US\";i:1;s:2:\"19\";}

I want to unserialize it during the mysql

8条回答
  •  难免孤独
    2020-12-02 13:52

    As mentioned by kchteam, MySQLToolBox library comes handy for this purpose using a custom defined MySQL function getPhpSerializedArrayValueByKey available here https://github.com/KredytyChwilowki/MySQLToolBox/blob/master/getPhpSerializedArrayValueByKey.sql.

    After adding this function, you can retrieve any value in the serialized array by using the following syntax,

    SELECT getPhpSerializedArrayValueByKey(column_name, 'array_key') AS deseializedArrayValue FROM table_name
    

    The given array can be unserialized like,

    SELECT getPhpSerializedArrayValueByKey('a:2:{i:0;s:2:"US";i:1;s:2:"19";}
    ', 'key_to_retrieve') AS key_to_retrieve
    

提交回复
热议问题