GROUP BY in Postgres - no equality for JSON data type?

后端 未结 1 1607
你的背包
你的背包 2020-12-11 00:39

I have the following data in a matches table:

5;{\"Id\":1,\"Teams\":[{\"Name\":\"TeamA\",\"Players\":[{\"Name\":\"AAA\"},{\"Name\":\"BBB\"}]},{\"Name\":\"Tea         


        
相关标签:
1条回答
  • 2020-12-11 01:39

    Shorter, faster and more elegant with a LATERAL join:

    SELECT DISTINCT ON (t.team->>'Name') t.team
    FROM   matches m, json_array_elements(m.match->'Teams') t(team);
    ORDER  BY t.team->>'Name', m.id DESC;  -- to get the "last"
    

    If you just want distinct teams, the ORDER BY can go. Related:

    • Query for element of array in JSON column
    • Query for array elements inside JSON type

    JSON and equality

    There is no equality operator for the json data type in Postgres, but there is one for jsonb (Postgres 9.4+):

    • How to query a json column for empty objects?
    0 讨论(0)
提交回复
热议问题