Received: from dealer.netventures.com.au (dnet137107-1.gw.connect.com.au [210.8.15.221] (may be forged)) by postgresql.org (8.11.3/8.11.4) with ESMTP id g1AMhdE51248 for ; Sun, 10 Feb 2002 17:43:40 -0500 (EST) (envelope-from david@netventures.com.au) Received: from ciderbox.in.netventures.com.au (net137107-2.gw.connect.com.au [210.9.198.119]) by dealer.netventures.com.au (Postfix) with ESMTP id 4AF841A64D7 for ; Mon, 11 Feb 2002 09:43:39 +1100 (EST) Date: Mon, 11 Feb 2002 09:43:57 +1100 Subject: Re: SQL Help - multi values Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v480) From: David Stanaway To: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit In-Reply-To: <5.1.0.14.2.20020210063242.00b18da0@bunce.bitecomm.co.uk> Message-Id: X-Mailer: Apple Mail (2.480) X-Archive-Number: 200202/104 X-Sequence-Number: 6532 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