Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id CF510474EB0 for ; Wed, 31 Jul 2002 11:57:27 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 4AF7FD609; Wed, 31 Jul 2002 08:57:32 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 409D85C04; Wed, 31 Jul 2002 08:57:32 -0700 (PDT) Date: Wed, 31 Jul 2002 08:57:32 -0700 (PDT) From: Stephan Szabo To: Mindaugas Riauba Cc: Subject: Re: SQL syntax In-Reply-To: <06d401c23894$7d9b4c00$f20214ac@B027543> Message-ID: <20020731085358.X17229-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200207/459 X-Sequence-Number: 8837 On Wed, 31 Jul 2002, Mindaugas Riauba wrote: > > I have two similar tables with host and services availability > data (Nagios). And I want to find out services which are not OK > in first table and OK in second one. Query I used is: > > select c.* from coll_servicestatus as c inner join > servicestatus as s on (c.service_description=s.service_description > and c.host_name=s.host_name) where c.service_status != 'OK' and > s.service_status = 'OK'; > > Results are fine. But how to write UPDATE statement if I want > to set those non-OK states in first table to OK if they are OK > in the second table? > > Something like: > > update coll_servicestatus set service_status = 'OK' from > coll_servicestatus as c, servicestatus as s where > c.service_description = s.service_description and c.host_name=s.host_name > and c.service_status != 'OK' and s.service_status = 'OK'; > > updates all rows not only required ones. There are two joins on coll_servicestatus in the above, the one in the from and one with the updating table. You could probably remove the coll_servicestatus as c, and change references to c to coll_servicestatus or use a subselect, something like: update coll_servicestatus set service_status='OK' where service_status!='OK' and exists (select * from servicestatus as s where coll_servicestatus.service_description=s.service_description and coll_servicestatus.host_name=s.host_name and s.service_status='OK');