在 SQL 中是不可能的,但是......
你可以在 PostgreSQL 中做到这一点
如果您只想要 id、字段名称(键)和值,您可以编写:
select d.id, e.key, e.value
from the_data d, jsonb_each_text(to_jsonb(d.*)) e
where value::integer = -1;
如果您想要该行,您可以:
select *
from the_data
where id in (
select d.id
from the_data d, jsonb_each_text(to_jsonb(d.*))
where value::integer = -1
);
请参阅以下运行示例:http ://rextester.com/CRGXPS45970
已编辑
您可以过滤字段或您想要的内容。例如:
select d.id, e.key, e.value
from the_data d, jsonb_each_text(to_jsonb(d.*)) e
where value::integer = -1 and key like 'a%';
select *
from the_data
where id in (
select d.id
from the_data d, jsonb_each_text(to_jsonb(d.*))
where value::integer = -1 and key like 'a%'
);
你可以在这里看到它:http ://rextester.com/EESKX21438