pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Stanaway <david@netventures.com.au>
To: pgsql-sql@postgresql.org
Subject: Re: SQL Help - multi values
Date: Mon, 11 Feb 2002 09:43:57 +1100
Message-ID: <AB55732C-1E77-11D6-8C55-0003930FDAB2@netventures.com.au> (raw)
In-Reply-To: <5.1.0.14.2.20020210063242.00b18da0@bunce.bitecomm.co.uk>


On Sunday, February 10, 2002, at 05:34  PM, James Carrier wrote:
>
> Surely there must be a better way than this - but it escapes me. In this
> setup the only way to match a specifc value when searching is to use 
> the query:
> SELECT dataid,title FROM table WHERE category LIKE '%|4|%'
> Which obviously has a huge performance penalty - and of course you can't
> JOIN against any of these values.
>
> The only other way I thought of was to use a separate table for the
> category entries:
>
> SELECT dataid,title,category FROM table LEFT JOIN table_categories ON
> table.dataid=table_categories.dataid
>
> But in the example above this would return 3 entries, which I don't 
> want,
> and I can't select a particular dataid which satisfies more than 
> category,
> e.g. has categories 4 and 7 (i.e. for the example above the LIKE 
> statement
> would be: WHERE category LIKE '%|4|%' AND LIKE '%|7|%').

create table mytab (myid serial, title text);
create table mytabcats (mytabid int REFERENCES mytab(myid), category 
int);
insert INTO mytab (title) values('Title A');
insert INTO mytabcats (mytabid,category) values(1,10);
insert INTO mytabcats (mytabid,category) values(1,11);
insert INTO mytabcats (mytabid,category) values(1,12);
insert INTO mytab (title) values('Title B');
insert INTO mytabcats (mytabid,category) values(2,20);
insert INTO mytabcats (mytabid,category) values(2,21);
insert INTO mytabcats (mytabid,category) values(2,22);
insert INTO mytabcats (mytabid,category) values(2,100);
insert INTO mytabcats (mytabid,category) values(1,100);

select distinct myid, title from mytab left join mytabcats ON myid = 
mytabid where category in (10,100);
-- myid |  title
--------+---------
--    1 | Title A
--    2 | Title B


==============================
David Stanaway
Personal: david@stanaway.net
Work: david@netventures.com.au




view thread (7+ messages)  latest in thread

Message-ID: <AB55732C-1E77-11D6-8C55-0003930FDAB2@netventures.com.au>
Permalink:  ../AB55732C-1E77-11D6-8C55-0003930FDAB2@netventures.com.au/
Also on:    postgresql.org/message-id/AB55732C-1E77-11D6-8C55-0003930FDAB2@netventures.com.au

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: david@netventures.com.au
  Subject: Re: SQL Help - multi values
  In-Reply-To: <AB55732C-1E77-11D6-8C55-0003930FDAB2@netventures.com.au>
  References: <5.1.0.14.2.20020210063242.00b18da0@bunce.bitecomm.co.uk> <AB55732C-1E77-11D6-8C55-0003930FDAB2@netventures.com.au>

* 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