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

时间:2016-02-23 15:03:31

标签: mysql node.js sequelize.js

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

表格是:

Questions: id, question
Answers: id, question_id, answer

我的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'],
    }]
})

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 

结果:

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

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

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

非常感谢Sequelize代码或MySQL查询的任何指针都能获得这样的结果。谢谢!

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

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

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` 

哪会回来,例如:

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

1 个答案:

答案 0 :(得分: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