X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A0E0B3A4B08 for ; Fri, 26 Nov 2004 23:06:25 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 74169-01 for ; Fri, 26 Nov 2004 23:06:14 +0000 (GMT) Received: from office.realtyserver.com (vpn1.realtyserver.com [142.179.100.60]) by svr1.postgresql.org (Postfix) with ESMTP id 18B8D3A3BA2 for ; Fri, 26 Nov 2004 23:06:14 +0000 (GMT) Received: from [192.168.1.184] ([192.168.1.184]) by office.realtyserver.com with Microsoft SMTPSVC(6.0.3790.211); Fri, 26 Nov 2004 15:23:02 -0800 Message-ID: <41A7B668.8050904@markreid.org> Date: Fri, 26 Nov 2004 15:04:08 -0800 From: m User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.3) Gecko/20040910 X-Accept-Language: en-us, en MIME-Version: 1.0 To: aarni@kymi.com Cc: pgsql-sql@postgresql.org Subject: Re: Querying a list field against another list References: <200411261617.57381.aarni@kymi.com> In-Reply-To: <200411261617.57381.aarni@kymi.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-OriginalArrivalTime: 26 Nov 2004 23:23:02.0937 (UTC) FILETIME=[E0344890:01C4D40E] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/277 X-Sequence-Number: 19595 Try this: CREATE OR REPLACE FUNCTION csv_matches(TEXT, TEXT) RETURNS BOOLEAN AS $$ DECLARE -- $1 is the field text, $2 is the list of ints to try and match. m TEXT; f TEXT; i INTEGER := 1; j INTEGER; BEGIN IF $1 IS NULL THEN RETURN 'f'; ELSIF $2 IS NULL THEN RETURN 'f'; END IF; LOOP m := split_part($2, ',', i); IF m LIKE '' THEN RETURN 'f'; END IF; j := 1; LOOP f := split_part($1, ',', j); IF f LIKE '' THEN EXIT; END IF; IF f LIKE m THEN RETURN 't'; END IF; j := j + 1; END LOOP; i = i + 1; END LOOP; END; $$ LANGUAGE 'plpgsql'; Then you can do "select * from foo where csv_matches(da_list, '1,4');" -Mark. Aarni Ruuhimäki wrote: >Hi, > >I tried to mail this to the novice list I believe it was rejected: > > > >The original message was received at 2004-11-26 14:55:09 +0100 >from postoffice.local [10.0.0.1] > > ----- The following addresses had permanent fatal errors ----- > > > -----Transcript of session follows ----- >... while talking to postoffice.local.: > > >>>>RCPT To: >>>> >>>> ><<< 550 5.1.1 unknown or illegal alias: jens@headlong.se >550 ... User unknown > > > >So here's my question. > >Hi people, > >This is not quite a pg question, but any suggestions are most welcome. > >How can one query a list of values against a db field that contains a list of >values ? > > >Table foo > >foo_id | foo_name | da_list >-------------------------------------- >1 | x | 1,2,3,4,5 >2 | y | 1,4,5 >3 | z | 4,5,11 >4 | xyz | 14,15,33 > >As a result from another query I have parameter bar = '1,4' and want to find >all rows from foo where da_list contains '1' or '4'. So loop over bar to loop >over da_list in foo ? > >My humble thanks, > >Aarni > >-------------- >This is a bugfree broadcast to you >from **Kmail** >on **Fedora Core 2** linux system >-------------- > >---------------------------(end of broadcast)--------------------------- >TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) > >