运算符不存在:json = json

Operator does not exist: json = json

当我尝试 select 来自 table

的一些记录时
    SELECT * FROM movie_test WHERE tags = ('["dramatic","women", "political"]'::json)

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

我是不是错过了什么或者我可以从哪里了解到这个错误。

简而言之 - 使用 JSONB 代替 JSON 或将 JSON 转换为 JSONB。

您无法比较 json 值。您可以改为比较文本值:

SELECT * 
FROM movie_test 
WHERE tags::text = '["dramatic","women","political"]'

但是请注意,JSON 类型的值以给定的格式存储为文本。因此比较的结果取决于您是否始终使用相同的格式:

SELECT 
    '["dramatic" ,"women", "political"]'::json::text =  
    '["dramatic","women","political"]'::json::text      -- yields false!
    

在 Postgres 9.4+ 中,您可以使用类型 JSONB 解决此问题,它以分解的二进制格式存储。可以比较此类型的值:

SELECT 
    '["dramatic" ,"women", "political"]'::jsonb =  
    '["dramatic","women","political"]'::jsonb           -- yields true

所以这个查询更可靠:

SELECT * 
FROM movie_test 
WHERE tags::jsonb = '["dramatic","women","political"]'::jsonb

详细了解 JSON Types