Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Wednesday December 25, 2002
02:15 AM

Stupid postgresql trick

[ #9622 ]

I created a table that held a key.

From that key I wanted to construct a URL. Instead of storing the URL in the table, I created a view.

CREATE VIEW urls (url) as SELECT 'http://www.example.com/' || key || '.png' FROM records;

Now, from that, I sometimes want to select a random record. A quick Google search tells me that I can order the results randomly---very cool. I use another view to give the query a name.

CREATE VIEW random_url as SELECT * FROM urls ORDER BY random() LIMIT 1;

Now I do not have to remember too much, or write a Perl function to do all of this. I just ask for a random URL.

select * from random_url;

Postgres is cool.