Click here to Skip to main content
15,899,634 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am having 10 columns or fields
s.no ans1 ans2 ans3 ... ans10
1 a b c x


i want a sqlite code to select a random column

What I have tried:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1
Posted
Updated 11-Oct-18 3:24am
Comments
Mohibur Rashid 11-Oct-18 6:31am    
Why?
Ruban Bharath 11-Oct-18 6:33am    
why means i cannot get you?

It is
SQL
SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

See SQLite Random[^].
 
Share this answer
 
Comments
Richard Deeming 11-Oct-18 9:38am    
Surely that would return a random row, when the OP said they wanted to return a random column?

Although confusingly, the "What I have tried" code seems to contradict the question. :)
CPallini 11-Oct-18 9:45am    
Look at the question title: "[...] random value from a column". Such a sentence agrees with the OP attempt. You know, we must go on with guesswork :-)
Use the rand()* method to get a random value between 1 and 10 inclusive. Create a SELECT clause by concatenating the number to the base column id, and pass that to the database. Something like:
column = rand(1..10)
string select = "SELECT ans" + column + "remainder of select clause"


* you have not mentioned what language you are using so the above is only pseudocode.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900