我使用knex查询我的后端
knex('project AS p')
.select('p.name as projectName')
.where('p.name', 'abc')
如果我使用p.name = 'abc'
,则代码有效
但是使用时出现错误:
.where(projectName, 'abc')
.where(p.name, 'abc')
.where({projectName: abc})
我想使用别名查询不是p.name,因为前端发送请求时,为什么要发送?projectName ='abc'而不是'p.name'='abc'。你知道怎么做
示例
SELECT
cp.id AS cId,
(CASE WHEN cp.id = 7 THEN 'abc' ELSE 'NOT abc' END) AS abc
FROM customer_project AS cp
;
我怎么这样查询
SELECT
cp.id AS cId,
(CASE WHEN cp.id = 7 THEN 'abc' ELSE 'NOT abc' END) AS abc
FROM customer_project AS cp
WHERE abc = 'abc'
;
Got错误列abc不退出。但是我真的想在abc列上查询(尽管它不会在架构上退出,但我创建了它)
答案 0 :(得分:0)
使用.toSQL()
来检查knex
会产生什么样的查询。您发布的示例无法正常运行(甚至存在语法错误),因此我无法确切地说出您在做什么错。
但是,它也可以与knex一起正常工作:
> const Knex = require('knex');
undefined
> const knex = Knex({
... client: 'pg',
... });
undefined
>
> console.log(
... knex('project AS p')
... .select('p.name as projectName')
... .where('p.name', 'abc')
... .where('projectName', 'abc')
... .toSQL()
... );
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 'abc', 'abc' ],
__knexQueryUid: 'c1a458e3-9fc4-4f9b-a206-eee7ecb5b897',
sql: 'select "p"."name" as "projectName" from "project" as "p" where "p"."name" = ? and "projectName" = ?' }
undefined
>
对我来说,生成SQL看起来还不错。