如何在postgresql中的jsonb列中进行搜索
我基本上有一个postgresql表,它有一个jsonb类型的列。json数据如下所示如何在postgresql中的jsonb列中进行搜索,postgresql,jsonb,Postgresql,Jsonb,我基本上有一个postgresql表,它有一个jsonb类型的列。json数据如下所示 { "personal": { { "gender":"male", "contact":{ "home":{ "email":"ceo@home.me", "phone_number":"5551234" }, "work":{
{
"personal": {
{
"gender":"male",
"contact":{
"home":{
"email":"ceo@home.me",
"phone_number":"5551234"
},
"work":{
"email":"ceo@work.id",
"phone_number":"5551111"
}
},
"religion":"other",
"languages":[
"English",
"Xen"
],
"last_name":"Eeo",
"birth_date":"1945-07-28",
"first_name":"Cee",
"nationality":"Martian",
"marital_status":"married"
}
}
}
我想把所有拥有“火星人”和“人族”国籍的人都找来。。在我的postgresql命令行中,这是有效的
select employees->'personal'->'contact'->'work'->'email'
from employees
where employees->'personal' @> '{"nationality":"Martian"}'
or employees->'personal' @> '{"nationality":"Terran"}'
这很有效。。但是它很难看。。我想运行这样的程序:
select employees->'personal'->'contact'->'work'->'email'
from employees
where employees->'personal'->'nationality' in ('Martian','Terran')
但我会遇到如下格式错误:
DETAIL: Token "Martian" is invalid.
CONTEXT: JSON data, line 1: Martian
必须使用“get value as text”(获取值为文本)操作符才能实现此操作:
select employees->'personal'->'contact'->'work'->>'email'
from employees
where employees->'personal'->>'nationality' in ('Martian','Terran')
我还将它添加到了获取电子邮件中,因为我假设您希望它作为文本
请注意,转换为text(雇员->'personal'->'national'):text
将不起作用,因为它不仅返回值,还返回转换为文本的json,在本例中,它是“Martian”
,包括引号。您必须使用“get value as text”操作符来实现这一点:
select employees->'personal'->'contact'->'work'->>'email'
from employees
where employees->'personal'->>'nationality' in ('Martian','Terran')
我还将它添加到了获取电子邮件中,因为我假设您希望它作为文本
请注意,转换为text(员工->'personal'>'national'):text
将不起作用,因为它不仅返回值,还返回转换为文本的json,在本例中,它是“Martian”
,包括引号。使用:
使用:
新问题(相关)新问题(相关)