Knex.js的官方文档中是这样示例的:
knex('users').orderBy([ { column: 'email' }, { column: 'age', order: 'desc' } ]) //等同于: select * from `users` order by `email` asc, `age` desc
Knex.js的官方文档
我是这样应用的:
const fundItem = await mysql('fund_items') .select('sum_share','sum_amount') .where('fund_id',fund_id) .orderBy([ { column: 'date' }, { column: 'id', order: 'desc' } ]).first() //等同于: select `sum_share`, `sum_amount` from `fund_items` where `fund_id` = 1 order by `[object Object]`, `[object Object]` asc limit 1
结果报错,因为orderBy里面的内容被翻译成了[object Object], [object Object]:
Error: ER_BAD_FIELD_ERROR: Unknown column '[object Object]' in 'order clause'
const fundItem = await mysql('fund_items') .select('sum_share','sum_amount') .where('fund_id',fund_id) .orderBy('date','asc').orderBy('id','desc').first() //等同于 select `sum_share`, `sum_amount` from `fund_items` where `fund_id` = ? order by `date` asc, `id` desc limit
这个问题是knex版本的问题,当前版本还不支持多项查询。所有我们通过连接orderBy来查询,效果都是一样的,代码如下: