【问题标题】:Knex.js divide value of a column by another columnKnex.js 将一列的值除以另一列
【发布时间】:2017-03-31 05:13:51
【问题描述】:

您好,我正在搜索如何使用 knex 创建这样的查询

SELECT product.price/100 AS priceInDollars

并收到错误“未找到价格/100”

相关问题divide the value of a column by another column

【问题讨论】:

    标签: sql knex.js


    【解决方案1】:

    Knex 似乎将列括在引号中,因此使用 Knex 查询构建器无法支持此类操作,因为数据库会将其解释为文字。

    knex.column('title', 'author', 'year').select().from('books')
    Outputs:
    select `title`, `author`, `year` from `books`
    

    但是,knex 还提供了一种触发原始 SQL 语句的方法,因此您可以执行此查询。

    knex.raw('SELECT product.price/100 AS priceInDollars').then(function(resp) { ... });
    

    延伸阅读:Knex Raw Queries

    【讨论】:

      猜你喜欢
      • 2012-01-05
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多