Skip site navigation (1) Skip section navigation (2)

Re: Postgresql function which compares values from both tables

From: "David Johnston" <polobo(at)yahoo(dot)com>
To: "'Richard Huxton'" <dev(at)archonet(dot)com>
Cc: "'Rehan Saleem'" <pk_rehan(at)yahoo(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Postgresql function which compares values from both tables
Date: 2012-03-19 22:14:53
Message-ID: 00dc01cd061d$b6381780$22a84680$@yahoo.com (view raw or flat)
Thread:
Lists: pgsql-sql
> -----Original Message-----
> From: pgsql-sql-owner(at)postgresql(dot)org [mailto:pgsql-sql-
> owner(at)postgresql(dot)org] On Behalf Of Richard Huxton
> Sent: Monday, March 19, 2012 4:59 PM
> To: David Johnston
> Cc: 'Rehan Saleem'; pgsql-sql(at)postgresql(dot)org
> Subject: Re: [SQL] Postgresql function which compares values from both
> tables
> 
> On 19/03/12 20:04, David Johnston wrote:
> > You would need to install the "HSTORE" extension to convert the record
> > into a key->value pair then perform the comparison on that.
> 
> No, you can do this just fine with a simple join.
> 
> BEGIN;
> 
> CREATE TABLE ta (id int PRIMARY KEY, d date, t text); CREATE TABLE tb (id
int
> PRIMARY KEY, d date, t text);
> 
> INSERT INTO ta
> SELECT 100+n, '2001-01-01'::date + n, 'entry ' || n FROM
> generate_series(1,19) n;
> 
> INSERT INTO tb
> SELECT 200+n, '2001-01-01'::date + n, 'entry ' || n FROM
> generate_series(1,19) n;
> 
> SELECT ta.id AS id_a, tb.id AS id_b, ta.d, ta.t FROM ta JOIN tb USING
(d,t);
> 
> ROLLBACK;
> 
> If the fields were named differently you'd need something like:
>    FROM ta JOIN tb ON (ta.d1, ta.t1) = (tb.d2, tb.t2)
> 

I think I read into the question too much.  If you want to find matching
rows then ( ROW(alias_1.*) = ROW(alias_2.*) ) will work (and you do not even
have to specify the columns explicitly).

Read this for details.  Note that if any of the columns can be NULL you need
to be especially careful.

http://www.postgresql.org/docs/9.0/interactive/functions-comparisons.html

I was thinking that you wanted to know which specific columns matched even
if the row as a whole did not.

Dave J.



In response to

pgsql-sql by date

Next:From: Jayadevan MDate: 2012-03-20 03:21:10
Subject: Re: Postgresql function which compares values from both tables
Previous:From: Richard HuxtonDate: 2012-03-19 20:59:23
Subject: Re: Postgresql function which compares values from both tables

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group