Question_ID | Question_sentence
1 | This is a apple?
2 | This is a car?
3 | what number is this?
这是我的Mysql表,其中包含Question ID
和Question Sentence
,
当我创建一个页面并显示这些问题时,我想随机显示 ALL 这个问题,我该如何解决?
可能会添加一个新的列名Random_ID
,并会在0到9999之间分配一个随机数,
然后使用以下SQL语句
SELECT * FROM Tbl_Question Order By Random_ID
如何在select语句中添加新列并分配一个随机数?
或
有更好的解决方案吗?
答案 0 :(得分:3)
MySQL中有一个RAND()函数,类似于FLOOR(RAND() * (<max> - <min> + 1)) + <min>
。
因此,假设您要生成1到500之间的随机数,则可以点击
之类的查询SELECT t.*,
Floor(Rand() * 500) + 1 AS Random_ID
FROM tbl_question t
ORDER BY random_id
这应该有效。一切顺利。