Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id C305D1337B64 for ; Thu, 21 Oct 2010 05:10:14 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 57720-01 for ; Thu, 21 Oct 2010 08:10:07 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from ibook.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by mail.postgresql.org (Postfix) with ESMTP id 2956C1337B2F for ; Thu, 21 Oct 2010 05:10:06 -0300 (ADT) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by ibook.office.archonet.com (Postfix) with ESMTP id E2ED57C1A3; Thu, 21 Oct 2010 09:10:05 +0100 (BST) Message-ID: <4CBFF55D.1060200@archonet.com> Date: Thu, 21 Oct 2010 09:10:05 +0100 From: Richard Huxton User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.12) Gecko/20100915 Lightning/1.0b1 Thunderbird/3.0.8 MIME-Version: 1.0 To: Nicholas I CC: pgsql-sql@postgresql.org Subject: Re: Need help on update. References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.918 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RDNS_DYNAMIC=0.982 X-Spam-Level: X-Archive-Number: 201010/49 X-Sequence-Number: 34700 On 21/10/10 08:43, Nicholas I wrote: > Hi, > > there are two tables, table1 and table2, each having same column name > called sn_no,name. i want to update table1 names with table2 where sn_no > are same. > > select * from table1; > sn_no | name > -------+----------- > 1 | ramnad > 2 | bangalore > 3 | chennai > > > select * from table2; > sn_no | name > -------+----------- > 1 | Hyderabad > 2 | Delhi > 3 | Bombay > > Any help ? > > I tried with , some of the queries like, Close. This is surprisingly difficult in standard SQL. PostgreSQL has a (non-standard) FROM clause you can use though. BEGIN; CREATE TABLE table1 (sn int, nm text); CREATE TABLE table2 (sn int, nm text); INSERT INTO table1 VALUES (1,'ramnad'),(2,'bangalore'),(3,'chennai'); INSERT INTO table2 VALUES (1,'Hyderabad'),(2,'Delhi'),(3,'Bombay'); UPDATE table1 SET nm = table2.nm FROM table2 WHERE table1.sn = table2.sn; SELECT * FROM table1; ROLLBACK; Be careful with aliasing the target of the update (table1 in this case). As another poster has discovered, that counts as another table in your join. -- Richard Huxton Archonet Ltd