如何在mysql中更改查询,使其随机检索30行

aiqt4smr  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(507)

我想在我的wordpress插件中更改这个查询,以便它从100行中随机检索30行。

$query = $wpdb->prepare("
    SELECT
        p.*, qq.quiz_id, qq.question_order AS order
    FROM
       {$wpdb->posts} p 
    INNER JOIN
       {$wpdb->prefix}learnpress_quiz_questions qq ON p.ID = qq.question_id
    WHERE
       qq.quiz_id IN (" . join( ',', $format ) . ")
    AND
       p.post_status = %s
", $args );

我把它改成了这个,但还是不起作用。有人能帮我吗?

SELECT
    p.*, qq.quiz_id, qq.question_order AS order
FROM
    {$wpdb->posts} p 
INNER JOIN
    {$wpdb->prefix}learnpress_quiz_questions qq ON p.ID = qq.question_id
WHERE
    qq.quiz_id IN (" . join( ',', $format ) . ")
AND
    p.post_status = %s
ORDER BY
    RAND() LIMIT 30
3lxsmp7m

3lxsmp7m1#

将您的排序更改为以下内容:

order by RAND() * 30

或者如果您有一个整数id:

order by RAND() * id

相关问题