'Select the Rows having ID stored in Serialized Data in the table Column

I am trying to search the rows in my table data let say in,

table name: post_meta

having the id 18767

in column meta_value

where in another column meta_key = mwb_cpb_products_related

I am aware that, above can be achieve simply from WHERE clause but the problem is targeted value is in serialized i.e: a:2:{i:0;i:18756;i:1;i:18767;}

i did some research and found we can achieve this via REGEXP and also with SUBSTRING_INDEX

  1. so for REGEXP i followed this tutorial https://www.namasteui.com/search-from-serialize-field-in-mysql-database/

and try below query by removing array_key as we are not sure what would be the array key, but got No results,

SELECT * FROM `frbl1ozme_postmeta` WHERE `meta_key` = 'mwb_cpb_products_related' AND `meta_value` REGEXP '.*;s:[0-9]+:".18756.".*' 
  1. Then i tried SUBSTRING_INDEX as brief in this tutorial https://polevaultweb.com/2020/02/selecting-data-serialized-array-mysql/

and tried following edited query with SUBSTRING, but got same No results

SELECT *  FROM `frbl1ozme_postmeta` WHERE `meta_key` = 'mwb_cpb_products_related' AND SUBSTRING(meta_value, ( INSTR( meta_value, CONCAT( '18756', '";' ) ) + CHAR_LENGTH( '18756') + 1 ) )

I am sure I am doing some mistake, need experts advice to search.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source