node.js - 如何在 knex 的 where 子句中使用值数组?
问题描述
我如何使用值数组在 where 子句中进行比较,如下面的代码所示,“frompersons”是第一次调用时响应的名称数组,我想从“chatterusers”数据库中获取他们的信息。但是我怎样才能在下一个 where 子句中使用这个数组呢?
return knex('frndrqst').where({ toperson: toperson })
.select('fromperson')
.then(frompersons => {
db.select('*').from('chatterusers')
.where( 'name', '=', frompersons )
.then(data => {
res.json(data);
})
.catch(err => res.json("Unable to load frndrqsts !!!"))
})
.catch(err => res.json("Unable to load frndrqsts !!!"))
解决方案
//get the list of name from 'formperson' table
var subquery = knex.select('name').from('fromperson');
//get in all information from 'chatterusers table' that name is equal with name
return knex.select('*').from('chatterusers')
.whereIn('name', subquery)
输出 :
select * from chatterusers where name in (select name from fromperson)
推荐阅读
- sass - 循环数组sass并创建css
- html - 用 lastpass 确认密码
- vue.js - vue中如何使用props和watch?
- android - Android导航组件中的覆盖片段
- reactjs - 使用深度链接反应导航 - 访问子操作和参数的问题
- c# - c# 数据验证下拉列表中未处理的异常
- reactjs - 错误:无法读取未定义的属性“mountComponent”
- android - 如何修复设备与 Nexus 7 Android OS 版本 5.1.1 播放商店上当前版本的应用程序问题不兼容
- java - Micronaut 中的 DI 错误出现在运行时而不是编译时?
- python-3.x - Odoo 11 根据条件在同一模型的树视图之间切换