Re: foreach statment?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Abdul-wahid Paterson <aw(at)lintrix(dot)net>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: foreach statment?
Date: 2003-05-19 23:23:40
Message-ID: 20030519162038.S43727-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On 19 May 2003, Abdul-wahid Paterson wrote:

> Hi,
>
> I have a situation (that I seem to come across often - hence the
> question) where I have 3 tables.
>
> items
> items_options
> options
>
> items has a many-to-many relationship with options so items_options is
> merely a link table with an item_id and and option_id.
>
> What I need to do is give a default option to all items that don't have
> any options.
>
> I would do something like this:
>
> select i.item_id from items i where (select count(item_id) from
> items_options where item_id=i.item_id) = 0;
>
> And then write a script that will go through the outputted list of
> item_id's and for each one do an insert statement like:
>
> insert into items_options values ($item_id, $n);

Maybe something like (not really tested)

insert into items_options select item_id, $n from
items i where not exists (select item_id from item_options where
i.item_id=item_id);

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-05-19 23:35:27 Re: disk space usage enlarging despite vacuuming
Previous Message Dennis Gearon 2003-05-19 23:19:38 If you can't write it yourself, buy it, the story of microsoft