SQL 运算符不存在:json = json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32843213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Operator does not exist: json = json
提问by Isaac
when I try to select some record from a table
当我尝试从表中选择一些记录时
SELECT * FROM movie_test WHERE tags = ('["dramatic","women", "political"]'::json)
The sql code cast a error
sql代码抛出错误
LINE 1: SELECT * FROM movie_test WHERE tags = ('["dramatic","women",...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
********** 错误 **********
ERROR: operator does not exist: json = json
SQL 状态: 42883
指导建议:No operator matches the given name and argument type(s). You might need to add explicit type casts.
字符:37
Did I miss something or where I can learn something about this error.
我是不是错过了什么,或者我可以在哪里了解这个错误。
回答by klin
You cannot compare json values. You can compare text values instead:
您无法比较 json 值。您可以比较文本值:
SELECT *
FROM movie_test
WHERE tags::text = '["dramatic","women","political"]'
Note however that values of type json
are stored as text in a format in which they are given. Thus the result of comparison depends on whether you consistently apply the same format:
但是请注意,type 的值以json
它们给出的格式存储为文本。因此,比较的结果取决于您是否始终应用相同的格式:
SELECT
'["dramatic" ,"women", "political"]'::json::text =
'["dramatic","women","political"]'::json::text -- yields false!
In Postgres 9.4+ you can solve this problem using type jsonb
, which is stored in a decomposed binary format. Values of this type can be compared:
在 Postgres 9.4+ 中,您可以使用 type 解决这个问题jsonb
,它以分解的二进制格式存储。可以比较这种类型的值:
SELECT
'["dramatic" ,"women", "political"]'::jsonb =
'["dramatic","women","political"]'::jsonb -- yields true
so this query is much more reliable:
所以这个查询更可靠:
SELECT *
FROM movie_test
WHERE tags::jsonb = '["dramatic","women","political"]'::jsonb
Read more about JSON Types.
阅读有关JSON 类型的更多信息。