MySQL加入,ORDER BY RAND()然后排序ASC(最好使用Sequelize)

fir*_*asd 6 mysql node.js sequelize.js

我想对这个MySQL查询提供一些帮助.理想情况下,我使用node.js Sequelize ORM生成它.

表格是:

Questions: id, question
Answers: id, question_id, answer
Run Code Online (Sandbox Code Playgroud)

我的Sequelize代码是:

models.questions.findAll({
  where: {
    id: {
      $notIn: not_in
    }
  },
    order: [['id','ASC'], [models.answers, 'id', 'ASC']],
    attributes: ['id', 'question'],
    include: [{
        model: models.answers,
        attributes: ['id', 'question_id', 'answer'],
    }]
})
Run Code Online (Sandbox Code Playgroud)

随着not_in设置为-1,Sequelize生成此查询:

SELECT `questions`.`id`, 
       `questions`.`question`, 
       `answers`.`id`          AS `answers.id`, 
       `answers`.`question_id` AS `answers.question_id`, 
       `answers`.`answer`      AS `answers.answer` 
FROM   `questions` AS `questions` 
       LEFT OUTER JOIN `answers` AS `answers` 
                    ON `questions`.`id` = `answers`.`question_id` 
WHERE  `questions`.`id` NOT IN ( -1 ) 
ORDER  BY `questions`.`id` ASC, 
          `answers`.`id` ASC 
Run Code Online (Sandbox Code Playgroud)

并导致:

id  |   question    |   answers.id  |   answers.question_id |   answers.answer
13  |   first question  |   17  |   13  |   1st answer
13  |   first question  |   23  |   13  |   2nd answer
13  |   first question  |   24  |   13  |   3rd answer
14  |   second question |   18  |   14  |   1st answer
14  |   second question |   21  |   14  |   2nd answer
14  |   second question |   22  |   14  |   3rd answer
15  |   third question  |   19  |   15  |   1st answer
15  |   third question  |   20  |   15  |   2nd answer
Run Code Online (Sandbox Code Playgroud)

我想要这个结果,但问题是随机排序的.

因此,而不是13,14和15,它可能是14,15,13,但答案仍然与他们的问题一致并排序answers.id.

非常感谢Sequelize代码或MySQL查询的任何指针,以获得这样的结果.谢谢!

我尝试过ORDER BY RAND()在各个地方添加,但最终也会改变答案.

PS顺便说一句,早些时候我只需要随机挑选一个问题,我用过这个问题:

SELECT `questions`.`id`       AS `question_id`, 
       `questions`.`question` AS `question`, 
       `answers`.`id`         AS `answer_id`, 
       `answers`.`answer`     AS `answer` 
FROM   (SELECT `questions`.`id`, 
               `questions`.`question` 
        FROM   `questions` AS `questions` 
        WHERE  (SELECT `question_id` 
                FROM   `answers` AS `answers` 
                WHERE  `questions`.`id` = `answers`.`question_id` 
                       AND questions.id NOT IN ( -1 ) 
                LIMIT  1) IS NOT NULL 
        ORDER  BY RAND() 
        LIMIT  1) AS `questions` 
       INNER JOIN `answers` AS `answers` 
               ON `questions`.`id` = `answers`.`question_id` 
ORDER  BY `answers`.`question_id`, 
          `answers`.`id` 
Run Code Online (Sandbox Code Playgroud)

哪个会返回,例如:

id  |   question    |   answers.id  |   answers.question_id |   answers.answer
14  |   second question |   18  |   14  |   1st answer
14  |   second question |   21  |   14  |   2nd answer
14  |   second question |   22  |   14  |   3rd answer
Run Code Online (Sandbox Code Playgroud)

Ric*_*mes 1

在普通 MySQL 中:

SELECT  ...
    FROM  
      ( SELECT  RAND() AS rnd, id FROM questions ) AS r
    JOIN  questions AS q ON q.id = r.id
    JOIN  answers AS a   ON a.question_id = q.id
    ORDER BY  r.rnd, a.id
Run Code Online (Sandbox Code Playgroud)