Re: join with 1 row limit

From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: join with 1 row limit
Date: 2011-01-16 11:17:23
Message-ID: iguk43$m38$1@reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On 2011-01-15, Behringer Behringer <behringerdj(at)yahoo(dot)com> wrote:
> Sorry for that
>
>> > SELECT *
>> > FROM
>> > products p LEFT JOIN
>> > products_images pi ON p.id = pi.product_id  LIMIT
>> 1
>> > WHERE products.company = 7
>
> I just want to get one row from images table for each product row from products table.

SELECT distinct on (p.id)
*
FROM products p
LEFT JOIN products_images pi
ON p.id = pi.product_id WHERE products.company = 7

or perhaps:

SELECT *
FROM products p LEFT JOIN
( SELECT * FRPM products_images pi where p.id = pi.product_id limit
1) as foo ON TRUE
WHERE products.company = 7

an

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Josh Kupershmidt 2011-01-16 17:15:49 Re: async fast-path calls?
Previous Message Behringer Behringer 2011-01-15 18:37:54 Re: join with 1 row limit