在PostgreSQL中,可以使用Knex.raw中的"where"作为来自子查询的"where"的数据。具体步骤如下:
const knex = require('knex')({
client: 'pg',
connection: {
host: 'your_host',
user: 'your_username',
password: 'your_password',
database: 'your_database'
}
});
const subquery = knex('orders')
.select('user_id')
.where('product_id', '=', 'your_product_id');
const query = knex('users')
.whereRaw('id IN ?', subquery);
query.then((results) => {
console.log(results);
}).catch((error) => {
console.error(error);
});
在上面的代码中,我们首先创建了一个子查询subquery,它选择了所有购买了特定产品的用户ID。然后,我们在主查询中使用Knex.raw的"whereRaw"方法,将子查询作为参数传递给它。这样就可以将子查询的结果用作主查询的"where"条件。
请注意,上述示例中的"your_host"、"your_username"、"your_password"、"your_database"和"your_product_id"应替换为实际的数据库连接信息和产品ID。
这是一个使用Knex.raw的"where"作为来自子查询的"where"的数据的示例。希望对你有帮助!如果你需要更多关于Knex.js和PostgreSQL的信息,可以参考腾讯云的云数据库PostgreSQL产品文档:腾讯云云数据库PostgreSQL。
领取专属 10元无门槛券
手把手带您无忧上云