I am using PostgreSQL 9.4 with a teams table containing a jsonb column named json . I'm looking for a query where I can get all the teams that have players 3 , 4 and 7 in their array of players.
The table contains two rows with the following json data:
First line:
{ "id": 1, "name": "foobar", "members": { "coach": { "id": 1, "name": "A dude" }, "players": [ { "id": 2, "name": "B dude" }, { "id": 3, "name": "C dude" }, { "id": 4, "name": "D dude" }, { "id": 6, "name": "F dude" }, { "id": 7, "name": "G dude" } ] } }
second line:
{ "id": 2, "name": "bazbar", "members": { "coach": { "id": 11, "name": "A dude" }, "players": [ { "id": 3, "name": "C dude" }, { "id": 5, "name": "E dude" }, { "id": 6, "name": "F dude" }, { "id": 7, "name": "G dude" }, { "id": 8, "name": "H dude" } ] } }
What should the query look like to get the desired list of commands? I tried a request where I would create an array of participating members jsonb_array_elements(json -> 'members' -> 'players')->'id' and compared them, but all I could do was result when any of player identifiers being compared was available in the team, and not in all of them.