Skip to content
Advertisement

select random COLUMN from PostgeSQL

I have a table which contains some links to photos in a few columns and I need to get a random photo from it.

gives me a 3 links. But can I get only 1 random out of it?

Advertisement

Answer

You can unpivot. For instance:

If the columns can ever be NULL, then you might want WHERE v.photo IS NOT NULL.

I should note that using ORDER BY RANDOM() is an expensive operation. You are using that in your query and so performance does not seem to be an issue.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement