分钟 -.min(column|columns|raw, [options])

获取指定列或列数组的最小值(请注意,某些驱动程序不支持多列)。也接受原始表达式。


knex('users').min('age')

输出:

select min(`age`) from `users`

knex('users').min('age', {as: 'a'})

输出:

select min(`age`) as `a` from `users`

knex('users').min('age as a')

输出:

select min(`age`) as `a` from `users`

knex('users').min({ a: 'age' })

输出:

select min(`age`) as `a` from `users`

knex('users').min({ a: 'age', b: 'experience' })

输出:

select min(`age`) as `a`, min(`experience`) as `b` from `users`

knex('users').min('age', 'logins')

输出:

select min(`age`) from `users`

knex('users').min({ min: ['age', 'logins'] })

输出:

select min(`age`, `logins`) as `min` from `users`

knex('users').min(knex.raw('??', ['age']))

输出:

select min(`age`) from `users`

最大 —.max(column|columns|raw, [options])

获取指定列或列数组的最大值(请注意,某些驱动程序不支持多列)。也接受原始表达式。


knex('users').max('age')

输出:

select max(`age`) from `users`

knex('users').max('age', {as: 'a'})

输出:

select max(`age`) as `a` from `users`

knex('users').max('age as a')

输出:

select max(`age`) as `a` from `users`

knex('users').max({ a: 'age' })

输出:

select max(`age`) as `a` from `users`

knex('users').max('age', 'logins')

输出:

select max(`age`) from `users`

knex('users').max({ max: ['age', 'logins'] })

输出:

select max(`age`, `logins`) as `max` from `users`

knex('users').max({ max: 'age', exp: 'experience' })

输出:

select max(`age`) as `max`, max(`experience`) as `exp` from `users`

knex('users').max(knex.raw('??', ['age']))

输出:

select max(`age`) from `users`

总和 -.sum(column|columns|raw)

检索给定列或列数组的值的总和(请注意,某些驱动程序不支持多列)。也接受原始表达式。


knex('users').sum('products')

输出:

select sum(`products`) from `users`

knex('users').sum('products as p')

输出:

select sum(`products`) as `p` from `users`

knex('users').sum({ p: 'products' })

输出:

select sum(`products`) as `p` from `users`

knex('users').sum('products', 'orders')

输出:

select sum(`products`) from `users`

knex('users').sum({ sum: ['products', 'orders'] })

输出:

select sum(`products`, `orders`) as `sum` from `users`

knex('users').sum(knex.raw('??', ['products']))

输出:

select sum(`products`) from `users`

使用sumDistinct在聚合函数内添加一个独特的表达式。


knex('users').sumDistinct('products')

输出:

select sum(distinct `products`) from `users`

平均 -.avg(column|columns|raw)

检索给定列或列数组的平均值(请注意,某些驱动程序不支持多列)。也接受原始表达式。


knex('users').avg('age')

输出:

select avg(`age`) from `users`

knex('users').avg('age as a')

输出:

select avg(`age`) as `a` from `users`

knex('users').avg({ a: 'age' })

输出:

select avg(`age`) as `a` from `users`

knex('users').avg('age', 'logins')

输出:

select avg(`age`) from `users`

knex('users').avg({ avg: ['age', 'logins'] })

输出:

select avg(`age`, `logins`) as `avg` from `users`

knex('users').avg(knex.raw('??', ['age']))

输出:

select avg(`age`) from `users`

使用avgDistinct在聚合函数中添加一个独特的表达式。


knex('users').avgDistinct('age')

输出:

select avg(distinct `age`) from `users`