Inserting Multiple Random Rows

From: Gary Chambers <gwchamb(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Inserting Multiple Random Rows
Date: 2010-04-28 16:22:23
Message-ID: q2x302670f21004280922ic25b4f91k968effc6926a6243@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

All,

I have a table of user addresses that I'm trying to randomly populate
with data from a cities table. Using the following query:

INSERT INTO useraddrs(userid, addrdesc, city, stprov)
SELECT u.userid, 'Home', c.cityname, c.stateabbr
FROM users u, cities c
WHERE u.userid NOT IN (SELECT userid FROM useraddrs)
AND cid=(SELECT cid FROM cities ORDER BY RANDOM() LIMIT 1);

I am able to achieve most of what I am trying to accomplish, but once
the random number is selected, it doesn't change. What am I missing?
Thank you in advance.

-- Gary Chambers

/* Nothing fancy and nothing Microsoft! */

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2010-04-28 17:05:58 Re: Inserting Multiple Random Rows
Previous Message Leif Biberg Kristensen 2010-04-27 14:18:57 Re: regexp_replace and search/replace values stored in table