Re: storing record-array in varchar?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: me(at)alternize(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: storing record-array in varchar?
Date: 2006-01-13 00:58:57
Message-ID: 20060113005857.GA51677@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Fri, Jan 13, 2006 at 12:11:40AM +0100, me(at)alternize(dot)com wrote:
> how can something like this be achieved:
>
> UPDATE mytable SET m_list = (SELECT DISTINCT s_id FROM sometable
> WHERE s_id > 6000) WHERE m_id = 10;

In 7.4 and later, if m_list were an array then you could do this:

UPDATE mytable SET m_list = ARRAY(SELECT ...) WHERE ...

> the field m_list would then hold something like '6001, 6002, 6003, 7000',
> which ideally i could later use for something like SELECT * FROM sometable
> JOIN mytable WHERE s_id IN m_list AND m_id = 10;

Again, if m_list were an array then you could do something like this:

SELECT *
FROM sometable AS s
JOIN mytable AS m ON s.s_id = ANY(m.m_list)
WHERE m_id = 10;

> field m_list would idealy be a varchar if possible...

Why? It could be done but an array seems more suitable for what
you're describing.

Is there a reason you want

m_id | m_list
------+-----------------------
10 | {6001,6002,6003,7000}

instead of the more conventional

m_id | s_id
------+------
10 | 6001
10 | 6002
10 | 6003
10 | 7000

?

--
Michael Fuhr

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message fearff@yahoo.com 2006-01-13 01:32:34 moving db off a rooted server
Previous Message me 2006-01-12 23:11:40 storing record-array in varchar?