B
B
Bogdan2017-07-28 11:41:25
PostgreSQL
Bogdan, 2017-07-28 11:41:25

Is the last record in the master table related to the child?

Hello. Do not prompt how correctly to implement. You need to select the last record in the parent1 table and then all the records from the child1 table related by key.
Or is there a better solution? Thanks.

SELECT  "product_id", "amount" FROM"child1" WHERE "child1"."parent_id" = (SELECT  "product_id", "amount" FROM "parent1"ORDER BY "parent1"."id" DESC LIMIT 1)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mishania666, 2017-07-28
@mishania666

SELECT *
FROM (
  SELECT  "product_id", "amount"
  FROM "parent1"
  ORDER BY "parent1"."id" DESC 
  LIMIT 1
) t
UNION
SELECT  "product_id", "amount" 
FROM "child1" 
WHERE "child1"."parent_id" = (SELECT "product_id" FROM "parent1"ORDER BY "parent1"."id" DESC LIMIT 1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question