Node.js 如何从Knex.js中的联接表中选择特定列?
我在SQL数据库方面工作了大约20年,似乎很难理解Knex映射查询的方法。有人能帮我找到正确的密码吗 我想在我的nodejs应用程序中使用以下SQL查询:Node.js 如何从Knex.js中的联接表中选择特定列?,node.js,knex.js,Node.js,Knex.js,我在SQL数据库方面工作了大约20年,似乎很难理解Knex映射查询的方法。有人能帮我找到正确的密码吗 我想在我的nodejs应用程序中使用以下SQL查询: SELECT p.id, p.project_number, p.project_name, p.start_date, p.end_date, o.name, o.email_addr, c.company, c.email_addr AS company_email, c.first_name, c.last_name FRO
SELECT p.id, p.project_number, p.project_name, p.start_date, p.end_date,
o.name, o.email_addr,
c.company, c.email_addr AS company_email, c.first_name, c.last_name
FROM projects p
INNER JOIN owners o ON o.id = p.owner_id
INNER JOIN clients c ON c.id = p.client_id
KnexJS(0.7.5)文档显示了我的查询的示例:
knex.from('projects').innerJoin('owners', 'owners.id', 'projects.owner_id')
.innerJoin('clients', 'clients.id', 'projects.client_id');
有两件事我在文档中找不到:
1) 如何选择要包括哪些列?项目、客户和业主都有20到50个专栏,我对它们都不感兴趣。从主表中选择列很清楚(使用select()或column()),但是如何从联接的表中选择列
2) 有些列具有相同的名称。如何避免名称冲突(即为其他表中的列添加一些前缀)?我研究了Knex生成列别名(…AS…)的方法,但我不确定它是否适合更复杂的查询。(即使是像上面这样相对简单的查询)您也可以尝试:
knex('projects').select(['projects.id', 'projects.project_number', 'projects.project_name', 'projects.start_date', 'projects.end_date',
'owners.name', 'owners.email_addr as owner_email_addr',
'clients.company', 'clients.email_addr as client_email_addr', 'clients.first_name', 'clients.last_name'])
.innerJoin('owners', 'owners.id', 'projects.owner_id')
.innerJoin('clients', 'clients.id', 'projects.client_id');
希望这有帮助 在这个答案中,我将介绍更多内容,而不仅仅是选择和问题本身 选择和避免名称冲突 选择的原则是使用
select
。通过重命名列(别名),可以避免名称冲突。
要创建别名(列重命名),只需在列名字符串中使用as
,如下所示:
knex
.select([
'table_a.id',
'table_b.id as b_id' // ---> the way to create aliases (to avoid
]) // columns overlapping and overriding
.from('table_a')
.leftJoin(
'table_b',
'table_a.table_b_id',
'table_b.id'
);
它将生成以下SQL:
SELECT
"table_a"."id",
"table_b"."id" as "b_id"
FROM
"table_a"
LEFT JOIN "table_b" ON
"table_a"."table_b_id" = "table_b"."id"
我们可以用不同的方式来表述。下面是一些有趣的例子:
knex('processes') // <-- wihtout using from (shorter) [less sql like though]
.select([
'processes.id as processId',
'processes.name as processName',
'processes.description as processDescription',
'processes.deleted as processDeleted',
'processes.deleteTime as processDeleteTime',
'rsp.runningSettingId',
'rsp.value as settingValue',
'rsp.startTime as settingStartTime'
])
.innerJoin(
'runningSettings_processes as rsp',
'processes.id',
'rsp.processId'
);
使用回调进行进一步控制
knex
.select('*')
.from(function () {
this.select('*').from('A')
.where('id',1)
.as('t1');
})
.leftJoin(
knex('B').where('id',2).as('t2'),
function () {
this.on('t1.c', '=', 't2.d'); // <--- notice also the use of function
} // construct for the join condition
);
knex
。选择(“*”)
.from(函数(){
this.select('*')。from('A'))
.其中('id',1)
.as('t1');
})
.leftJoin(
knex('B')。其中('id',2)。as('t2'),
函数(){
this.on('t1.c','=','t2.d')//
knex(
knex('A').where('A.id',1).as('t1')
).leftJoin(
knex('B').where('B.id', 2).as('t2'),
't1.c',
't2.d'
)
//or filter the resulting table of the join
knex('A')
.leftJoin(
knex('B').where('B.id', 2).as('t2'),
't1.c',
't2.d'
)
.where('someColumn', 2);
knex
.select('*')
.from(function () {
this.select('*').from('A')
.where('id',1)
.as('t1');
})
.leftJoin(
knex('B').where('id',2).as('t2'),
function () {
this.on('t1.c', '=', 't2.d'); // <--- notice also the use of function
} // construct for the join condition
);
knex( // <--- notice the knex constructor (equivTo: knex.select().from())
knex('A') //NT: we already used that above when answering the question
.select('*')
.where('id',1)
.as('t1');
)
.leftJoin(
knex('B').where('id',2).as('t2'),
function () { // <----- notice that in place of using the 2nd and 3d arg, we can have a function in the 2nd arg
this.on('t1.c', '=', 't2.d').on('t1.s', '=', 't2.g'); // <-----|
} // plus: multiple on conditions______/
);