Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A815A9FAE4E for ; Tue, 3 Oct 2006 14:32:32 -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 99300-04 for ; Tue, 3 Oct 2006 14:32:23 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.181]) by postgresql.org (Postfix) with ESMTP id 73F199FA4F4 for ; Tue, 3 Oct 2006 14:32:23 -0300 (ADT) Received: by py-out-1112.google.com with SMTP id d80so3055218pyd for ; Tue, 03 Oct 2006 10:32:22 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=NbS0ejY8DHgIX68OrvLjrRXicUnsLVURRHmKIpGiEVSfdtwuA79mhxBAC79jNymmhrKTWTD9DWllpP8QbOYU8Qc/IsHK6nKeUJTym1U1yDrXKztuzdNnRfVMdU07gB3RVaLvqwU1wA91cGLNxYsxW8/WnI2gM+JbjCFnT32l54k= Received: by 10.35.135.12 with SMTP id m12mr10296077pyn; Tue, 03 Oct 2006 10:32:22 -0700 (PDT) Received: by 10.35.49.10 with HTTP; Tue, 3 Oct 2006 10:32:22 -0700 (PDT) Message-ID: Date: Tue, 3 Oct 2006 13:32:22 -0400 From: "Worky Workerson" To: pgsql-sql@postgresql.org Subject: Updating from table with multiple possibilities MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.802 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, INFO_TLD, SPF_PASS X-Spam-Level: X-Archive-Number: 200610/27 X-Sequence-Number: 26416 I have two tables, ips and ranges, where ips contains a list of ips (INET) and ranges contains a list of ip blocks (CIDR), ala: CREATE TABLE ips (ip INET, info varchar); CREATE TABLE ranges (range CIDR, info varchar); I would like to update the info column in ips to be the info column of the most specific block in ranges. For example, if ranges had the following data: 0.0.0.0/0, 'top level class' 18.0.0.0/8, 'MIT' 18.228.0.0/20, 'MIT Group B' and ips only had the row "'18.228.0.1', NULL" then doing an update would change the column in ips to "'18.228.0.1', 'MIT Group B'" I can't quite figure out the update syntax to get this to work. This obviously does not work (as mentioned in the manual), as there are multiple results returned from the 'join' and I only want the 'best' match: UPDATE ips SET info = ranges.info FROM ranges WHERE ip <<= range I figure that I have to use subselects, but I can't seem to figure out how to get the data out of the subselect (i.e. the info column). Do I need to join ips and ranges explicitly? Thanks!