我正在研究用于存储有关对象的无模式元数据的 EAV 存储系统,现在正在研究一种使其可搜索的方法,但大部分处理由数据库服务器完成。
EAV的表格是:
`entityID` - INT(11)
`entity` - VARCHAR(128)
`attribute` - VARCHAR(128)
`value` - BLOB
这是我的声明:
SELECT
`entity`,
(CASE WHEN `attribute` = 'bob' THEN `value` ELSE NULL END) `bob`,
(CASE WHEN `attribute` = 'dean' THEN `value` ELSE NULL END) `dean`
FROM `eav`
它返回一组不错的行(如预期的那样),如下所示:
+----------+------+------+
| entity | bob | dean |
+----------+------+------+
| testEnt | foo | NULL | // attribute = bob
+----------+------+------+
| testEnt | NULL | NULL | // another test attribute
+----------+------+------+
| testEnt | NULL | NULL | // another test attribute
+----------+------+------+
| testEnt2 | foo | NULL | // attribute = bob
+----------+------+------+
| testEnt2 | NULL | foo | // attribute = dean
+----------+------+------+
但是当我附加时GROUP BY (Entity)
,结果变成了这样:
+----------+------+------+
| entity | bob | dean |
+----------+------+------+
| testEnt | foo | NULL |
+----------+------+------+
| testEnt2 | foo | NULL |
+----------+------+------+
所以使用HAVING
之后的语法停止工作。有没有办法让返回的结果是:
+----------+------+------+
| entity | bob | dean |
+----------+------+------+
| testEnt | foo | NULL |
+----------+------+------+
| testEnt2 | foo | foo |
+----------+------+------+