所以我有一个这样的表和一个属性列表-( public JSONObject toJSON() {
JSONObject ret = new JSONObject();
ret.put("generatorClass", this.generatorClass);
ret.put("targetClass", this.targetClass);
ret.put("name", this.name);
:
:
return ret;
}
,'attr1'
,'attr2'
)。
'attr3'
我要查询一个可以将该表“扩展”为以下内容的查询,然后使用它进行查询。
id | attr_name | attr_value
----------------------------
1 | attr1 | val1
1 | attr2 | val2
1 | attr3 | val3
2 | attr1 | val1
2 | attr2 | val4
属性列表是动态的。
答案 0 :(得分:2)
交叉连接ID和属性名称,然后左连接表以获取属性值;如果找不到匹配项,则返回NULL
。
SELECT x1.id,
x2.attr_name,
t2.attr_value
FROM (SELECT DISTINCT
t1.id
FROM elbat t1) x1
CROSS JOIN (VALUES ('attr1'),
('attr2'),
('attr3')) x2 (attr_name)
LEFT JOIN elbat t2
ON t2.id = x1.id
AND t2.attr_name = x2.attr_name
ORDER BY x1.id,
x2.attr_name;
答案 1 :(得分:1)
WITH
ids AS ( SELECT DISTINCT id
FROM sourcetable ),
attrs AS ( SELECT DISTINCT attr_name
FROM sourcetable )
INSERT INTO sourcetable (id, attr_name)
SELECT id, attr_name
FROM ids, attrs
EXCEPT
SELECT id, attr_name
FROM sourcetable
答案 2 :(得分:0)
对不起,如果这看起来很明显,但是您尝试了
core.Reflect.getOwnMetadata
?
然后,当您查询表时,表中应包含值
编辑:您可能还想拥有一个唯一键