Accessing random rows from database without repetition

夙愿已清 提交于 2020-01-22 03:08:29

问题


I have designed a quiz scenario using MySQL database as my backend.

I have a total of 20 questions and I would want to display them in random order from the database. I have tried :

SELECT * from mst_que ORDER BY RAND();

What the above query does is repeat few rows.


回答1:


If the table contains duplicate records, use SELECT DISTINCT to filter them out.

SELECT DISTINCT *
FROM mst_que
ORDER BY RAND()



回答2:


The order by clausule needs column names or relative positions , not values or values. So. Try to add the RAND to the select and order by it. Try this:

SELECT *, RAND() as ordering
FROM mst_que
ORDER by ordering;


来源:https://stackoverflow.com/questions/49655053/accessing-random-rows-from-database-without-repetition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!