pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Aaron Bono <postgresql@aranya.com>
To: Stefan Arentz <stefan.arentz@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Querying for name/value pairs in reverse
Date: Sat, 15 Jul 2006 17:21:02 -0500
Message-ID: <bf05e51c0607151521v3385a6fey4eb5aa233e0a50e4@mail.gmail.com> (raw)
In-Reply-To: <adf34c300607150224k7d4243f6ye52a21bf0a1bd0a6@mail.gmail.com>
References: <adf34c300607150224k7d4243f6ye52a21bf0a1bd0a6@mail.gmail.com>

On 7/15/06, Stefan Arentz <stefan.arentz@gmail.com> wrote:
>
> I'm no SQL expert by any means so I'm wondering if something like this
> is possible.
>
> I have two tables like this:
>
> create table Errors (
>   Id serial not null,
>   CreateDate timestamp not null,
>   primary key (Id)
> );
>
> create table ErrorValues (
>   Id serial not null,
>   ErrorId int not null,
>   Name varchar(255) not null,
>   Value text not null,
>   primary key (Id),
>   foreign key (ErrorId) references Errors (Id)
> );
>
> Now, selecting specific ErrorValues with a bunch of names that are
> related to an Error is of course pretty simple. But I want to go the
> other way. I want to query for:
>
> 'give me all Errors that have the Code=1234 AND Address=1.2.3.4 AND
> Type=OSX Name/Value pairs'
>
> What is a good recipe to deal with this? Is something like this
> possible with standard sql? Is there a nice PG way to do this?


 Try this

SELECT
   Errors.ID,
   Errors.CreateDate
FROM Errors
WHERE Errors.ID IN (
   SELECT
      ErrorValues.id,
   FROM ErrorValues
   WHERE (ErrorValues.name = 'Code' AND ErrorValues.value = '1234')
   INTERSECT
   SELECT
       ErrorValues.id,
    FROM ErrorValues
    WHERE (ErrorValues.name = 'Address' AND ErrorValues.value = '1.2.3.4')
   INTERSECT
   SELECT
       ErrorValues.id,
    FROM ErrorValues
    WHERE (ErrorValues.name = 'Type' AND ErrorValues.value = 'OSX Name/Value
pairs')
);

==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
==================================================================

view thread (4+ messages)  latest in thread

Message-ID: <bf05e51c0607151521v3385a6fey4eb5aa233e0a50e4@mail.gmail.com>
Permalink:  ../bf05e51c0607151521v3385a6fey4eb5aa233e0a50e4@mail.gmail.com/
Also on:    postgresql.org/message-id/bf05e51c0607151521v3385a6fey4eb5aa233e0a50e4@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: postgresql@aranya.com, stefan.arentz@gmail.com
  Subject: Re: Querying for name/value pairs in reverse
  In-Reply-To: <bf05e51c0607151521v3385a6fey4eb5aa233e0a50e4@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox