Roberto C Roberto C - 1 year ago 54
PHP Question

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 wich contain serialized data in form of

. 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 wich contain some serialized data like
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 ?

Thank you !

Answer Source

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