Stupid postgresql trick

brian_d_foy on 2002-12-25T07:15:19

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.