Re: Updating with a subselect

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Leandro Casadei <mateamargo(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Updating with a subselect
Date: 2008-04-23 13:59:56
Message-ID: 20080423065541.S588@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 22 Apr 2008, Leandro Casadei wrote:

> Hi, I need to update a field from a table based in a count.
>
> This is the query:
>
>
> update shops
> set itemsqty =
> (
> select count(*)
> from items i1
> join shops s1 on i1.shopid = s1.shopid
> where s1.shopid = s0.shopid
> )
> from shops s0

I think you'll actually want something simpler. The following might do
what you want.

update shops
set itemsqty =
(
select count(*)
from items i1
where i1.shopid = shops.shopid
)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Albe Laurenz 2008-04-23 14:05:54 Re: Deny creation of tables for a user
Previous Message wstrzalka 2008-04-23 13:58:48 Re: WAL shipping with archive_timeout & pg_switch_xlog()