Re: How to get n records from parent table and their children

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: JORGE MALDONADO <jorgemal1960(at)gmail(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: How to get n records from parent table and their children
Date: 2017-11-16 07:21:09
Message-ID: 1510816869.2639.14.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

JORGE MALDONADO wrote:
> I have a parent and child tables and need to get "n" records from parent table
> and all of the records in child for such "n" records in parent. What can be a good approach?

I'd try it as follows:

SELECT ...
FROM (SELECT p_id, ...
FROM parent
LIMIT 42) p
JOIN child c
USING (p_id);

This assumes that "p_id" is the key column in "parent"
and the foreign key column in "child".

Yours,
Laurenz Albe

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Laurenz Albe 2017-11-16 07:27:27 Re: group by rollup and cube
Previous Message john snow 2017-11-16 03:53:39 group by rollup and cube