Limit # of recs on inner join?

Lists: pgsql-sql
From: Josh <josh(at)globalherald(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Limit # of recs on inner join?
Date: 2007-12-31 18:52:03
Message-ID: alpine.LRH.1.00.0712311347330.31526@home-av-server.home-av
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql


I want to limit the number of records returned by an inner join.

Suppose I have a table of Books:

book_id
title

And, a table of authors:

book_id
author_name

Now, suppose I want to get book + author, but I only want one author for
books with multiple authors. Traditionally, I'd do something like:

select books.book_id, books.title, authors.author_name
from books
inner join authors on authors.book_id = books.book_id
where books.book_id = ?

This might return:

1 A Cat In The Hat Dr. Seuss
1 A Cat In The Hat Dr. Seuss' Partner

Instead, I just want:

1 A Cat In The Hat Dr. Seuss

How can I limit the inner join?

Cheers,
-J


From: Rodrigo E(dot) De León Plicet <rdeleonp(at)gmail(dot)com>
To: Josh <josh(at)globalherald(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Limit # of recs on inner join?
Date: 2008-01-01 02:07:05
Message-ID: a55915760712311807l716a77e8l86873515f0ca1f9@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Dec 31, 2007 1:52 PM, Josh <josh(at)globalherald(dot)net> wrote:
> Instead, I just want:
>
> 1 A Cat In The Hat Dr. Seuss

SELECT books.book_id, books.title,
(SELECT author_name
FROM authors
WHERE book_id = books.book_id
LIMIT 1) AS author_name
FROM books
WHERE books.book_id = ?


From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: pgsql-sql(at)postgresql(dot)org, Josh <josh(at)globalherald(dot)net>
Subject: Re: Limit # of recs on inner join?
Date: 2008-01-01 05:25:19
Message-ID: 195966.8694.qm@web31808.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

SELECT DISTINCT ON ( B.book_id, B.title ) B.book_id, B.title, A.Author
FROM Books AS B
INNER JOIN Authors AS A
ON A.book_id = B.book_id;

--- On Mon, 12/31/07, Josh <josh(at)globalherald(dot)net> wrote:

> From: Josh <josh(at)globalherald(dot)net>
> Subject: [SQL] Limit # of recs on inner join?
> To: pgsql-sql(at)postgresql(dot)org
> Date: Monday, December 31, 2007, 10:52 AM
> I want to limit the number of records returned by an inner
> join.
>
> Suppose I have a table of Books:
>
> book_id
> title
>
> And, a table of authors:
>
> book_id
> author_name
>
> Now, suppose I want to get book + author, but I only want
> one author for
> books with multiple authors. Traditionally, I'd do
> something like:
>
> select books.book_id, books.title, authors.author_name
> from books
> inner join authors on authors.book_id = books.book_id
> where books.book_id = ?
>
> This might return:
>
> 1 A Cat In The Hat Dr. Seuss
> 1 A Cat In The Hat Dr. Seuss' Partner
>
> Instead, I just want:
>
> 1 A Cat In The Hat Dr. Seuss
>
> How can I limit the inner join?
>
> Cheers,
> -J
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 5: don't forget to increase your free space map
> settings