Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (Postfix) with ESMTP id B020447598F for ; Fri, 19 Jul 2002 14:54:23 -0400 (EDT) Received: from [66.219.92.2] (HELO chocolate-mousse) by davinci.ethosmedia.com (CommuniGate Pro SMTP 3.5.9) with ESMTP id 1583685; Fri, 19 Jul 2002 11:54:22 -0700 Content-Type: text/plain; charset="iso-8859-1" From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: "Patrick Hatcher" , pgsql-sql@postgresql.org Subject: Re: Is it possible to use a field from another table as part of a query? Date: Fri, 19 Jul 2002 11:54:27 -0700 X-Mailer: KMail [version 1.4] References: In-Reply-To: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200207191154.27095.josh@agliodbs.com> X-Archive-Number: 200207/311 X-Sequence-Number: 8689 Partick, > I have a table that contains a VARCHAR field with data formatted as such: > 12,44,13,225 > what I would like to do is use this field in a query to another table such > as: 1. Since you are a smart guy, I'm assuming that this "delimited" VARCHAR da= ta=20 is legacy data that you don't have a choice about re-structuring. Because,= =20 of course, the normalized way to store the data would be in a subtable, not= a=20 VARCHAR field. >=20 > CREATE TABLE category_tree ( > tree varchar(200) > ) WITH OIDS; >=20 > Select * from mdc_products > where keyf_category_home IN (select tree from category_tree) >=20 > However, my keyf_category_home field is an INT4. Is there a way to parse > out the tree field so that I can define it as INT4? Well, this is easiest thing to do: Select * from mdc_products where EXISTS=20 (select tree from category_tree WHERE tree ~ ('(^|,)' || keyf_category_home || '(,|$)')); (somebody please correct my regexp if I've made an error) ... but that's impossible to index. If the table category_tree doesn't=20 change often, I'd write a program to parse the data and build a normalized= =20 subtable containg a vertical colum of tree values. --=20 -Josh Berkus Aglio Database Solutions San Francisco