X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 43D7E9FB21F for ; Fri, 28 Jul 2006 20:30:51 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 00690-05 for ; Fri, 28 Jul 2006 20:30:39 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from myemma.com (db1.e2ma.net [66.179.147.163]) by postgresql.org (Postfix) with ESMTP id 479299FB24B for ; Fri, 28 Jul 2006 20:30:38 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by myemma.com (Postfix) with ESMTP id F005C23CD4; Fri, 28 Jul 2006 18:34:05 -0500 (CDT) Received: from myemma.com ([127.0.0.1]) by localhost (velma [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 03232-04; Fri, 28 Jul 2006 18:34:05 -0500 (CDT) Received: from [192.168.2.14] (c-68-53-113-70.hsd1.tn.comcast.net [68.53.113.70]) by myemma.com (Postfix) with ESMTP id 9DD8523CD0; Fri, 28 Jul 2006 18:34:05 -0500 (CDT) Message-ID: <44CA9E18.8020306@myemma.com> Date: Fri, 28 Jul 2006 18:30:32 -0500 From: Erik Jones User-Agent: Thunderbird 1.5.0.5 (Windows/20060719) MIME-Version: 1.0 To: Fabian Peters Cc: pgsql-sql@postgresql.org Subject: Re: UPDATE with WHERE clause on joined table References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at myemma.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.183 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200607/365 X-Sequence-Number: 25939 Fabian Peters wrote: > Hi, > > I'm only very rarely using SQL, so please forgive me if I show any > obvious signs of ignorance... > > I've got three tables "customer", "address" and "country". I want to > set the "language" attribute on "customer" on rows returned by a > SELECT such as this: > > SELECT title, first_names, last_name, email, language, > country.country_name FROM ((customer JOIN address ON customer.eoid = > address.eoid_customer) JOIN country ON address.eoid_country = > country.eoid) WHERE email LIKE '%.es' AND country.country_name = 'SPAIN'; > > That is, I want to set the "language" to 'Spanish' where the > "customer.email" is like '%.es' and where "country.country_name" is > 'SPAIN'. > > I've tried all sorts of places to put the JOIN and the WHERE clauses > within the UPDATE statement, but I just don't get it. > > I'd be most grateful for any help... > > TIA > > Fabian > > P.S.: One of my sorry attempts looked like this - which updates all > rows in "customer" so I figure the WHERE clause is not where it should > be: > > UPDATE customer SET language = 'Spanish' FROM ((customer AS > customer_address JOIN address ON customer_address.eoid = > address.eoid_customer) JOIN country ON address.eoid_country = > country.eoid) WHERE customer.email LIKE '%.es' AND > country.country_name = 'SPAIN'); The FROM clause is where you put relations other than the one you are updating. Try this: UPDATE customer SET language='Spanish' FROM address ad, country co WHERE customer.eoid=ad.eoid_customer AND ad.eoid_country=co.eoid AND co.country_name='SPAIN' AND customer.email LIKE '%.es'; Note that for demonstration purposes I've aliased the join tables and that (unfortunately) you can't alias the update table. erik jones software development emma(r)