Re: more than one row returned by a subquery used as an expression

From: David Fetter <david(at)fetter(dot)org>
To: CSN <cool_screen_name90001(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: more than one row returned by a subquery used as an expression
Date: 2005-10-14 00:44:00
Message-ID: 20051014004400.GA8361@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Oct 13, 2005 at 05:05:56PM -0700, CSN wrote:
> I'm trying to get this query to work:
>
> update sectors set companies =(select companies from industries
> where sector_id =sectors.id);
>
> PG returns:
>
> ERROR: more than one row returned by a subquery used as an
> expression

It's doing what it should. Try the inner query for a few specific
examples of sector_id.

>
> Column companies is just a count of rows in the related companies
> table. Queries like this worked:
>
> update industries set companies =(select count(id) from companies
> where industry_id =industries.id);

The inner query is guaranteed to return exactly one row. The other
one is not.

> I know I could do a join in the subselect, but I'm curious why this
> doesn't work. If I do the subselect by itself, it looks like all of
> the rows from the industries table are return (I expected an error).
> Is this the "implicit FROM" gotcha?

Nope. Thinko on your end. Just make sure your inner query is
guaranteed to return exactly one row, and you'll be good. :)

Cheers,
D
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Marcos Wilceki 2005-10-14 00:58:16 Re: [ANNOUNCE] 8.1 Beta 3: The Final Beta?
Previous Message Guy Rouillier 2005-10-14 00:17:46 Re: PostgreSQL's bug tracker