mysql select query within a serialized array

前端 未结 14 1228
忘了有多久
忘了有多久 2020-11-29 07:51

I\'m storing a list of items in a serialized array within a field in my database (I\'m using PHP/MySQL).

I want to have a query that will select all the records that

相关标签:
14条回答
  • 2020-11-29 07:57

    You can do it like this:

    SELECT * FROM table_name WHERE some_field REGEXP '.*"item_key";s:[0-9]+:"item_value".*'
    

    But anyway you should consider storing that data in a separate table.

    0 讨论(0)
  • 2020-11-29 07:58

    Well, i had the same issue, and apparently it's a piece of cake, but maybe it needs more tests.

    Simply use the IN statement, but put the field itself as array! Example:

    SELECT id, title, page FROM pages WHERE 2 IN (child_of)
    

    ~ where '2' is the value i'm looking for inside the field 'child_of' that is a serialized array.

    This serialized array was necessary because I cannot duplicate the records just for storing what id they were children of.

    Cheers

    0 讨论(0)
  • 2020-11-29 07:59

    If I have attribute_dump field in log table and the value in one of its row has

    a:69:{s:9:"status_id";s:1:"2";s:2:"id";s:5:"10215"}
    

    If I want to fetch all rows having status_id is equal to 2, then the query would be

    SELECT * FROM log WHERE attribute_dump REGEXP '.*"status_id";s:[0-9]+:"2".*'
    
    0 讨论(0)
  • 2020-11-29 08:00

    So you mean to use MySQL to search in a PHP array that has been serialized with the serialize command and stored in a database field? My first reaction would be: OMG. My second reaction would be: why? The sensible thing to do is either:

    1. Retrieve the array into PHP, unserialize it and search in it
    2. Forget about storing the data in MySQL as serialized and store it as a regular table and index it for fast search

    I would choose the second option, but I don't know your context.

    Of course, if you'd really want to, you could try something with SUBSTRING or another MySQL function and try to manipulate the field, but I don't see why you'd want to. It's cumbersome, and it would be an unnecessary ugly hack. On the other hand, it's a puzzle, and people here tend to like puzzles, so if you really want to then post the contents of your field and we can give it a shot.

    0 讨论(0)
  • 2020-11-29 08:01

    As GWW says in the comments, if you need to query things this way, you really ought to be considering storing this data as something other than a big-ole-string (which is what your serialized array is).

    If that's not possible (or you're just lazy), you can use the fact that the serialized array is just a big-ole-string, and figure out a LIKE clause to find matching records. The way PHP serializes data is pretty easy to figure out (hint: those numbers indicate lengths of things).

    Now, if your serialized array is fairly complex, this will break down fast. But if it's a flat array, you should be able to do it.

    Of course, you'll be using LIKE '%...%', so you'll get no help from any indicies, and performance will be very poor.

    Which is why folks are suggesting you store that data in some normalized fashion, if you need to query "inside" it.

    0 讨论(0)
  • 2020-11-29 08:01
    select * from postmeta where meta_key = 'your_key'  and meta_value REGEXP  ('6')
    
    0 讨论(0)
提交回复
热议问题