我正在研究一種用於存儲有關對象的無模式元數據的EAV存儲系統,現在正在研究如何讓它可搜索,但大部分數據庫服務器都完成了處理。將SQL結果行合併到一起
的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 |
+----------+------+------+
並感謝您對John Woo – topherg