Operator does not exist: json = json

前端 未结 1 1591
广开言路
广开言路 2020-12-09 08:19

when I try to select some record from a table

    SELECT * FROM movie_test WHERE tags = (\'[\"dramatic\",\"women\", \"political\"]\'::json)

相关标签:
1条回答
  • 2020-12-09 09:17

    You cannot compare json values. You can compare text values instead:

    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:

    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:

    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.

    0 讨论(0)
提交回复
热议问题