How beauty to join a few tables Postgesql

I have 2 tables on my database: Order (id, name, price, user_id), UserData(id, name, surname); and Order table has a user_id column it is a UserData id; and I want to return the Order table with UserData in this way:

{
  id: 4,
  name: "milk",
  price: 100,
  user_id: 8,
  user_data: {
        id: 8,
        name: "John",
        surname: "Simson"
              }
  }

Is it possible to make this without a backend, only with using Postgres functions?

Upvotes: 0

Views: 29

Answers (1)

GMB
GMB

Reputation: 222432

Is this what you want?

select o.*, to_jsonb(ud) as user_data
from orders o
inner join user_data ud on ud.id = o.user_id

Upvotes: 1

Related Questions