Updating with a subselect

From: "Leandro Casadei" <mateamargo(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Updating with a subselect
Date: 2008-04-22 16:17:42
Message-ID: 5983afbb0804220917x3771415boc6299d0ba6d9f328@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

The problem I'm having is that all the shops are updated with the items
quantity of the first shop.
I've tried using the count without a subselect, but PostgreSQL complains
about it.

How can I do this?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Vanole, Mike 2008-04-22 17:04:27 Best approach for large table maintenance
Previous Message blackwater dev 2008-04-22 15:47:08 Re: can't cast char to in