查询和子查询中的续订顺序

时间:2017-02-27 03:18:42

标签: sequelize.js

我试图找出如何使用order属性来订购我的子查询和父查询。使用当前设置,order子句对父查询进行排序,但不对子查询进行排序。这可能吗?如果是这样,我怎么能实现它?

以下是我的完整查询:

models.Blog.findAll({
            order: 'blog_date DESC',
            limit: 10,
            include: [
            {
                model: models.Topic,
                attributes: ['topicName'],
                required: false
            },
            {
                model: models.BlogComment,
                include: [{
                    model: models.User,
                    attributes: ['userId','firstName', 'lastName']
                }],
                required: false
            }]
        })

生成查询

SELECT ...
FROM (SELECT ... FROM blog LIMIT 10)
LEFT OUTER JOIN topic ...
LEFT OUTER JOIN blog_comment ...
ORDER BY blog.blog_date DESC;

我希望加入以下ORDER BY

FROM (SELECT ... FROM blog ORDER BY blog.blog_date LIMIT 10)

1 个答案:

答案 0 :(得分:1)

您可以使用相同的方式使用sorder子查询,添加订单属性

models.Blog.findAll({
            order: 'blog_date DESC',
            limit: 10,
            include: [
            {
                model: models.Topic,
                attributes: ['topicName'],
                required: false
            },
            {
                model: models.BlogComment,
                include: [{
                    model: models.User,
                    attributes: ['userId','firstName', 'lastName'],
                    order : 'firstName DESC'
                }],
                required: false
            }]
        })

<强>更新

您需要尝试

order: [["blog_date","DESC"]]

而不是

order: 'blog_date DESC'

请参阅以下链接

Sequelize js - limit and Sorting bug