首页 > 解决方案 > 如何在 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 !!!"))

标签: node.jsexpress

解决方案


//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)

推荐阅读