Dear All,
I am using this following query to select questions randomly from database
sql query:
("select top 5 * from "+ practicallab +" where subtopic ='"+ sub_topic +"' order by newid()")
The above query is working to select questions randomly from database.
But my problem is,
If i want to select five questions randomly from twenty questions i can select using the above query.
(1,2,3,4,5,6,7........20)questions are there.
when i run the program like 5,9,15,18,20 question are displayed.
Again when i run the program 6,4,13,18,15 questions are displayed.
But when i run again and again one or two same questions are repeatedly displayed.
I want to display different five questions(randomly) each time.
what is the sql query for this process?
Arul ManivannanPosted Mar 28, 2013, 6:01 AM
If you want to display random records without repeating means, just follow the procedure following.
1. Create a column [TimeFetched] in the table.
2. Update Table set [TimeFetched] = getDate()
Thats all your table is ready.., now include the following query every time your program runs
2. Just select only top 5 records on the table order by TimeFetched asc
3. After Selecting those records update the TimeFetched value = getDate()
So when running the program again, it will ignore the latest selected and go for the other one which are not fetched now.
This scenario will work when you have only one user for your program. If there are multiple user the functionality may not work. For that you have to maintain a history on new table say UsersQuestionsHistory to store the userid and questionsfetched data. Based on that, you have to show the records when user demands.
You decide the choice,
Kindly intimate if my words are not clear.
Thanks
Arul Manivannan