Skip to content
Advertisement

How to select records from a Postgres table using an ORDER BY statement on a jsonb column that points to another table

I have two tables, characteristics and students.

characteristics

students

The characteristics table has three columns where the id column is an auto-increment field. The name denotes the name of a specific characteristic and the value indicates the respective information about the characteristic.

The students table contains detail about each student. The character_value field in the students table is a jsonb field. Each element in the jsonb array is an ID that points to the appropriate characteristic ID in the characteristics table.

(for example, if a student is from the country of india then the ID 5 is appended to character_values jsonb array against the respective student’s name)

Assuming that each student can only belong to one country, how do I select a list of students while ordering the result by the name of the country they belong to?

Advertisement

Answer

You can join the tables with jsonb_array_elements:

Output:

id name value first_name
1 country england Jason
1 country england Mark
3 games football Mark
3 games football Kunal
4 games baseball Jason
5 country india Kunal
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement