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'], }]})
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
And results in:
id | question | answers.id | answers.question_id | answers.answer13 | first question | 17 | 13 | 1st answer13 | first question | 23 | 13 | 2nd answer13 | first question | 24 | 13 | 3rd answer14 | second question | 18 | 14 | 1st answer14 | second question | 21 | 14 | 2nd answer14 | second question | 22 | 14 | 3rd answer15 | third question | 19 | 15 | 1st answer15 | third question | 20 | 15 | 2nd answer{
"questions":[
{
"id":13,
"question":"first question",
"answers":[
{
"id":17,
"question_id":13,
"answer":"1st answer"
},
{
"id":23,
"question_id":13,
"answer":"2nd answer"
},
{
"id":24,
"question_id":13,
"answer":"3rd answer"
}
]
},
{
"id":14,
"question":"Second question",
"answers":[
{
"id":18,
"question_id":14,
"answer":"1st answer"
},
{
"id":21,
"question_id":14,
"answer":"2nd answer"
},
{
"id":22,
"question_id":14,
"answer":"3rd answer"
}
]
},
{
"id":15,
"question":"Third question",
"answers":[
{
"id":19,
"question_id":15,
"answer":"1st answer"
},
{
"id":20,
"question_id":15,
"answer":"2nd answer"
}
]
}
]
}order: [[models.sequelize.fn('RAND'), ['id','ASC'], [models.answers, 'id', 'ASC']],
SELECT questions.id, questions.question, answers.id as `answers.id`, answers.question_id as `answers.question_id`, answers.answer as `answers.answer`
FROM
( SELECT RAND() AS rnd, id FROM questions where questions.id NOT IN (15) ) AS r
JOIN questions AS questions ON questions.id = r.id
JOIN answers AS answers ON answers.question_id = questions.id
ORDER BY r.rnd, answers.id