mysql 在序列化数组中选择查询 [英] mysql select query within a serialized array
问题描述
我将项目列表存储在数据库字段中的序列化数组中(我使用的是 PHP/MySQL).
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 contain a specific one of these items that is in the array.
像这样:
select * from table WHERE (an item in my array) = '$n'
希望这是有道理的.
任何想法将不胜感激.
谢谢
推荐答案
所以你的意思是使用 MySQL 在已经序列化的 PHP 数组中搜索 serialize 命令并存储在数据库字段中?我的第一反应是:天啊.我的第二反应是:为什么?明智的做法是:
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:
- 将数组检索到 PHP 中,对其进行反序列化并在其中进行搜索
- 忘记将数据以序列化形式存储在 MySQL 中,而是将其存储为常规表并为其建立索引以进行快速搜索
我会选择第二个选项,但我不知道您的背景.
I would choose the second option, but I don't know your context.
当然,如果您真的想要,您可以尝试使用 SUBSTRING
或其他 MySQL 函数并尝试操作该字段,但我不明白您为什么要这样做.这很麻烦,而且这将是一个不必要的丑陋黑客.另一方面,它是一个谜题,这里的人们往往喜欢谜题,所以如果你真的想然后发布你的领域的内容,我们可以试一试.
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.
这篇关于mysql 在序列化数组中选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!