gpt4 book ai didi

MySQL 查询获取随机问题

转载 作者:行者123 更新时间:2023-11-29 20:55:21 25 4
gpt4 key购买 nike

我有两个名为类别和问题的表。我需要帮助编写查询以获取随机问题。如果有 10 个问题,第一个将来自第一类,第二个将来自第二类,以此类推,直到可以显示 10 个问题。我的表结构如下所示

类别编号 |姓名

1 | php

2 | jQuery

3 | MySQL

问题编号 |问题|答案1 |答案2 |猫迪德 |正确

1 | q1 | a1 | a2 | 1 | a1

2 | q2 | a1 | a2 | 3 | a1

我的查询是这样的

select * from questions where category_id=$category_id ORDER BY RAND() LIMIT 12

最佳答案

我用我的公式解决了问题。这可能很难,但我解决了。你可以试试....

//代码从这里开始

//Get maximum value of category id
$cid = "select * from category";//Getting category id
$cid = mysqli_query($con,$cid);//Run the query
$v = array();//Create an array
while($d = mysqli_fetch_array($max)){
array_push($v,$d['id']);//Push all id to the array.
}
//Get single key form the array randomly with php array function array_rand().
$rand = array_rand($v,1);
$rand = $v[$rand];//Ultimately get the random category id.

//Now use the category id in where clause.
$sql = "select * from questions where category_id=$rand LIMIT 12";
$sql = mysqli_query($con,$sql);
while($d = mysqli_fetch_array($sql)){//$d = data
echo $d['id'].'. '.$d['question'].'<br>';//Display the questions randomly with multiple questions if exists.
}

//代码结束谢谢。

关于MySQL 查询获取随机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37684621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com