Mysql EAV match row as field or entity's property value

元气小坏坏 提交于 2020-01-04 10:59:17

问题


My Mysql database has objects which are defined by an object type which is then joined to set it's properties. And it is based on the Entity Attribute Value model as pointed out by Spencer.

The database is a little more complicated then below but this should make my question a little more clear.

I have a mysql table objects which contains some basic information about an object say create_date, title,author and of course an id.

I also have a table object_properties which contains some more data about objects.

I have some trouble searching through them. For example, an object might has both an end_date and start_date as a seperate property. These would each have their own row in object_properties.

How would one match the end and start date as they each have their own row?

Normally I would query it like this; select * from table where start_date > value AND end_date < value But I can't do this because each property is a separate row.

I tried the following query; `

 select * 
  from object_properties 
 where object_id = 1 
   and (     (object_properties.type_id = 1)
         and (object_properties.value = 2) 
         and (     (object_properties.type_id = 2) 
               and (object_properties.value = 2)
             )
       )

In the example above the type_id = 1 would be the start date and type_id =2 would be the end date but this did not seem to work. I was thinking I am looking at it the wrong way.

I realize the title of my question might be wrong, I don't really know how storing data this way is called.

Here is the solution I used based on Matthews answer:

Here is what I ended up with: `

 SELECT   
      o.title,  
      o.id,  
      op1.value AS start_date,  
      op2.value AS end_date  
 FROM object o  
 JOIN object_meta_values op1 ON op1.object_id=o.id AND op1.object_type_meta_id=28  
 JOIN object_meta_values op2 ON op2.object_id=o.id AND op2.object_type_meta_id=29  
 WHERE object_type_id = 11  
 AND (12      <= op1.value AND op1.value <  16     )   
 OR (12       <  op2.value   AND op2.value   <= 16     )   
 OR (op1.value <= 12        AND 16        <= op2.value)  

`. Thanks alot!


回答1:


You can use multiple left joins to do this.

SELECT 
    o.create_date,
    o.title,
    o.author,
    op1.value AS start_date,
    op2.value AS end_date,
    ...
FROM object o
LEFT JOIN object_properties op1 ON op1.object_id=o.object_id AND op1.type_id=1
LEFT JOIN object_properties op2 ON op2.object_id=o.object_id AND op2.type_id=2

Values that do not have properties will end up being null.

This method you don't have to have a second query to get the properties either.

EDIT If you do not want to retrieve null values, then omit the left part of the left join.




回答2:


The quick answer is that since you need to pull two rows, one way to do that would be to modify your query to replace the AND with an OR, e.g.

select p.* 
  from object_properties p
 where p.object_id = 1 
   and (  
          (p.type_id = 1 and p.value = 2) 
       OR 
          (p.type_id = 2 and p.value = 2)
       )

(Obviously, this isn't the only way or isn't necessarily the best way to to retrieve the specified result set; but a query like this would get you the two rows.)

The type of data model you are dealing with is typically referred to as an "Entity - Attribute - Value" model, or EAV model for short.




回答3:


You mean to find records where the end and start date are the same? Or dou you just want a list of all Objects with his properties?

P.S. Why don't you save every property in a column in stead of a row? 1 property id pointing to the objects table and you are done...



来源:https://stackoverflow.com/questions/12269135/mysql-eav-match-row-as-field-or-entitys-property-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!