MySQL – select only cells which contain serialized data

I’m trying to retrieve a list of products from a database. I have the “products_in_cart” column which contain serialized data in form of a:2:{i:0;s:3:"125";i:1;s:2:"92";}. I only want to select the cells that contain serialized data as above, at least one product in cart. The column is of type “text NULL” so I did a select “from where IS NOT NULL” but I get fields that does not contain products also. I also found out that I have entries which contain some serialized data like a:0:{} but no key->value pairs, and I do not want to retrieve those.

Is there a way to select only rows that contain serialized data in the field “product” with at leas one key=>value pair?


You could add “where products_in_cart not like ‘%:{}'”. This would exclude any entry where there is no data between the brackets.

In the case that you may have both null and the above mentioned data you would need to use:

WHERE products_in_cart not like '%:{}'
and products_in_cart is not NULL

Leave a Reply

Your email address will not be published. Required fields are marked *