pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: pgsql-sql@postgresql.org
Subject: Re: How to enter lists into database: Problems with solution.
Date: Wed, 26 Sep 2001 10:40:26 -0700
Message-ID: <web-126053@davinci.ethosmedia.com> (raw)
In-Reply-To: <20010925223227.12000.qmail@web10106.mail.yahoo.com>

Frederick,

> "Mary Stuart" correctly. But such a query also
> seems to get results that contain only one
> of the search_attributes.
> e.g. a 32 "Peter Smith" who e.g. just has an entry
> 24 32 "hair" "brown" (and no mice hobby) is also
> found.
> I need to get only results that match the search
> completely.
> I would be happy if you could help me again.
> Thanks, Frederick

Oops. You are quite correct. Unfortunately, the query that you need is
somewhat more complicated:
SELECT people.people_id, people.name,
       people.address,
       people_attributes.attribute_name,
       people_attributes.attribute_value
FROM people, people_attributes,
       ( SELECT people_id, count(*) as match_count
         FROM people_attributes, search_attributes
         WHERE search_id = 31
         AND people_attributes.attribute_name =
             search_attributes.attribute_name
         AND people_attributes.attribute_value ~*
             search_attributes.attribute_value ) matches,
       ( SELECT count(*) as attribute_count
FROM search_attributes
         WHERE search_id = 31 ) searched
WHERE people.people_id = people_attributes.people_id
  AND people.people_id = matches.people_id
  AND matches.match_count = searched.attribute_count;

This structure will also allow you to search for, say, 4 out of 5 items
by changing the last line to:
  AND matches.match_count >= (searched.attribute_count - 1);

Also, if you re-arrange the query slightly, you can turn it into a view.
The trick is to have the search_id as an output column rather than a
WHERE clause item in the sub-selects.

Have fun!

-Josh

______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



Message-ID: <web-126053@davinci.ethosmedia.com>
Permalink:  ../web-126053@davinci.ethosmedia.com/
Also on:    postgresql.org/message-id/web-126053@davinci.ethosmedia.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: josh@agliodbs.com
  Subject: Re: How to enter lists into database: Problems with solution.
  In-Reply-To: <web-126053@davinci.ethosmedia.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