如何在mysql中更改查询,以便它随机检索30行

时间:2018-10-07 14:18:46

标签: mysql wordpress customization

我想在我的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

1 个答案:

答案 0 :(得分:-1)

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

order by RAND() * 30

或者如果您具有整数ID:

order by RAND() * id