怎么解决sequelize会在order中那个参数自动加上表的别名(已解决)
发布于 8 年前 作者 mosaic101 9675 次浏览 来自 问答

code

attributes: [
                sequelize.literal('SUM(stock) AS stock'),
                sequelize.literal('SUM(actualAmount) AS actualAmount'),
                sequelize.literal('SUM(differAmount) AS differAmount'),
                sequelize.literal('SUM(beforeInventorySum) AS beforeInventorySum'),
                sequelize.literal('SUM(afterInventorySum) AS afterInventorySum')
            ],
            group: 'GoodId',
            order: ['differAmount','desc']

sql

SELECT
	SUM(stock) AS stock,
	SUM(actualAmount) AS actualAmount,
	SUM(differAmount) AS differAmount,
	SUM(beforeInventorySum) AS beforeInventorySum,
	SUM(afterInventorySum) AS afterInventorySum
FROM
	`inventory_result_details` AS `InventoryResultDetail`
GROUP BY
	GoodId
ORDER BY
	`InventoryResultDetail`.`differAmount` DESC

sequelize版本3.2,我想针对分组后的数据再排序,sequelize会在order中那个参数自动加上表的别名,导致数据有问题

13 回复

sequlize.col(‘column’) From Noder

@zhengnz 这个我在issues里看到了,就是不知道怎么加asc、desc,官方api没找到,如果方便贴一下此方法api,谢谢

[sequelize.col(‘column’), ‘asc’] From Noder

@zhengnz 这个报错,version@3.2,不知道是不是版本问题

没记错的话应该写成order: [[differAmount','desc']]

@iyuq 跟 [‘differAmount’,‘desc’]没区别,无法解决我的问题!

@zhengnz SequelizeBaseError: ER_BAD_FIELD_ERROR: Unknown column ‘desc’ in ‘order clause’

那在order中直接用db.literal吧

来自酷炫的 CNodeMD

@mosaic101

models.Table.findAll({
    where: {
        column: 1
    },
	order: [
	     [models.sequelize.col('other_column'), 'desc']
	]
})

order: [[sequelize.literal("differAmount DESC")]]

@iyuq 解决了,3q!

@Jealee3000 解决了,3q!

回到顶部