简体   繁体   English

如何解析mySql表的单个字段中的多个值?

[英]How can I parse multiple values in a single field of a mySql table?

I am working with a real-estate database in mySql. 我正在使用mySql中的房地产数据库。 This is used with a wordpress site. 与wordpress网站一起使用。 Each property that is listed has related agents in a joined table. 列出的每个属性在联接表中都有相关的代理。 There may be more than 1 agent to a property. 一个属性可能有多个代理。 However, multiple agents are listed in a single field. 但是,在一个字段中列出了多个代理。 (So, join is 1-1). (因此,join是1-1)。 Here is an example 这是一个例子

a:2:{i:0;s:4:"1995";i:1;s:3:"260";}

So it appears that this is some sort of array implementation. 因此看来这是某种数组实现。 I don't have much experience with this, as it is a bad way of storing data in a relational DB anyway. 我对此没有太多经验,因为无论如何这都是在关系数据库中存储数据的一种糟糕方法。 In this example, there are 2 agents. 在此示例中,有2个代理。 I know that: 我知道:
- the first "2" probably refers to the number of agents. -第一个“ 2”可能表示代理的数量。
- "1995" and "260" are the record-ids of the agent's record. -“ 1995”和“ 260”是代理记录的记录ID。
How would I craft a SQL statement to extract these two record ids (and find related data in the main record) from php? 我如何制作一条SQL语句从php提取这两个记录id(并在主记录中找到相关数据)?
If this is a cumbersome solution, I'd want to create a new SQL view to make access easier. 如果这是一个麻烦的解决方案,我想创建一个新的SQL视图以简化访问。 Is this possible? 这可能吗?
If there are any, I can't find any php functions that interprets this field. 如果有的话,我找不到解释此字段的任何php函数。
Table redesign is not an option. 不能重新设计表。

Edit: 编辑:
As I was reading through the answers to a similar question as pointed out, I came to the conclusion that this is most probably impossible to do. 当我阅读所指出的类似问题的答案时,我得出的结论是,这很可能是不可能做到的。 And frankly ridiculous. 坦白地讲。
The solution entailed using the SUBSTRING_INDEX repeatedly. 解决方案需要反复使用SUBSTRING_INDEX。 How many times, could vary every time. 多少次,每次都可能有所不同。 Even if the number of values is specified, it may not be possible to accomplish in MySQL. 即使指定了值的数量,也可能无法在MySQL中完成。 I will have to work with the limitations that this creates. 我将不得不处理由此产生的限制。

The data format you mention is php's serialization format . 您提到的数据格式是php的序列化格式 Before JSON took over the world, Rasmus Lerdorf and the php creators came up with their own way of doing it. 在JSON接管世界之前,Rasmus Lerdorf和php创建者想出了自己的方法。

WordPress uses lots of this stuff. WordPress使用了很多这样的东西。

php's unserialize() function converts it from that string representation into an appropriate php object. php的unserialize()函数将其从该字符串表示形式转换为适当的php对象。

If you must, you can probably use MySQL string functions like LOCATE() and SUBSTRING() to extract certain values from those serialized strings. 如果必须,您可能可以使用MySQL字符串函数(LOCATE()SUBSTRING())从那些序列化的字符串中提取某些值。 But that will be a huge pain in the neck, and very specific to the kind of object that's serialized. 但这将是一个巨大的瓶颈,并且非常针对序列化的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM