我有一个查询,它根据Entity-Attribute-Value模型返回超过一百万行。请注意,每个实体可能有不同数量的属性,因此,我不能只查找行ID。
+----------+-----------+------------+
| EntityID | Attr_Name | Attr_Value |
+----------+-----------+------------+
| 1 | Age | 2 |
+----------+-----------+------------+
| 1 | Class | Spatial |
+----------+-----------+------------+
| 2 | Age | 3 |
+----------+-----------+------------+
| 2 | Class | Industrial |
+----------+-----------+------------+
| 3 | Class | Industrial |
+----------+-----------+------------+我需要过滤所有的EntityID根据他们的Class。在本例中,假设我需要所有属于EntityID的Industrial,我希望我的查询返回3-4-5行(所以所有与EntityID 2和3相关的行)。
我考虑在同一个查询和EntityID分组中使用一个子select,只查找where子句(WHERE EntityID = (subquery))中的所有Industrial,但根本没有效果。该查询包含大量的连接和联合,因此需要大量的时间。我愿意接受所有的建议,以一种更有效的方式(我肯定有)!
谢谢。
发布于 2018-07-09 13:06:20
您可以使用exists
select t.*
from t
where exists (select 1
from t t2
where t2.entityid = t.entityid and
t2.attr_name = 'Class' and
t2.attr_value = 'Industrial'
);https://stackoverflow.com/questions/51246261
复制相似问题