過濾EAV表我有2個表:與多個條件
表objects
:
object_id | object_group_id
表attributes
:
attr_id | attr_object_id | attr_property_id | attr_value
現在,我想所有object_id
其中object_group_id = 1
和過濾器兩個屬性:
(attr_property_id = 1 AND attr_value <= '100000')
AND
(attr_property_id = 2 AND attr_value > '2000')
我試圖建立一些疑問,比如:
SELECT * FROM objects as o
/* filter1 join */
INNER JOIN
attributes AS f1
ON
o.object_id = f1.attr_object_id
AND
f1.attr_property_id = 1
/* filter2 join */
INNER JOIN
attributes AS f2
ON
f1.attr_object_id = f2.attr_object_id
AND
f2.attr_property_id = 2
WHERE
o.object_group_id = 1
AND
f1.attr_value <= '100000'
AND
f2.attr_value > '2000'
...但還是不能讓我需要什麼。
你想要有1行attr_values或2行。請從你想要的結果中顯示一個樣本 –
我想爲每個對象獲得一行,這些屬性匹配屬性中的兩個鍵值條件。 – WarGasm
看看這個。它生成您的查詢數據透視表 http://stackoverflow.com/questions/33350524/pivot-a-table-and-display-n-ordered-time-series/33351008?noredirect=1#comment54499334_33351008 –