这是我正在努力实现的一个硬编码示例:
SELECT id FROM places
WHERE metadata->'route'='Route 23'
OR metadata->'route'='Route 22'
OR metadata->'region'='Northwest'
OR metadata->'territory'='Territory A';
元数据列是一个hstore列,我想根据另一个表中的另一个查询动态构建WHERE子句。该表可以是:
id | metadata
---------+----------------------------
1647 | "region"=>"Northwest"
1648 | "route"=>"Route 23"
1649 | "route"=>"Route 22"
1650 | "territory"=>"Territory A"
或
id | key | value
----+-------------+-------+---
1 | route | Route 23
2 | route | Route 22
3 | region | Northwest
4 | territory | Territory A
真的很重要,只是无论是什么都可以构建where子句。根据其他查询,它可能包含1到n个OR。
答案 0 :(得分:1)
使用第二个表(分发表)结束解决方案:
id | metadata
---------+----------------------------
1647 | "region"=>"Northwest"
1648 | "route"=>"Route 23"
1649 | "route"=>"Route 22"
1650 | "territory"=>"Territory A"
使用以下联接,@>查看places.metadata是否包含distributions.metadata
SELECT places.id, places.metadata
FROM places INNER JOIN distributions
ON places.metadata @> distributions.metadata
WHERE distributions.some_other_column = something;