Skip to content
Advertisement

Getting data from three tables in postgresql

I’m new to postgresql. I have three tables

Table1: teacher

Table2: school_location

Table3: teachers_school_location

I want to write a query to get data from these three tables like this: (output in json by calling an API)

When user select tid = 1, I want output like this

Kindly look at this image

When user select tid = 2, I want output like this

Kindly look at this image

I used INNER JOINS to get an output (query is given below), but the problem is that data from teacher table is repetitive for each location.

Its output is:

output image of query

but I need an output like this:

desired output

How can I achieve this output?

Advertisement

Answer

as you know you can use built-in JSON functions in Postgres to prepare your JSON like so:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement