繁体   English   中英

如何在 knex.js 的 'join' 的 'on' 条件下使用 'and'

[英]how to use 'and' within the 'on' condition of 'join' in knex.js

我想在 knex.js 中实现以下 sql 代码:

select c.id,c.parent_id,c.comment,u.username,c.postid from comments as
     c join post_details as p on (p.id = c.postid and c.postid=15)join
     users as u on (u.id = c.userid);

我尝试这样做:

db('comments AS c')
  .join('post_details AS p', function () {
    this.on('p.id', '=', 'c.postid').on('c.postid', '=', db.raw('?', [postid]));
  })
  .join('users AS u', 'u.id', '=', 'c.userid')
  .select(['c.id', 'c.parent_id', 'c.comment', 'u.username', 'c.postid', 'c.userid'])
  .then((data) => {
    console.log(data);
    res.json(data);
  })
  .catch((err) => res.status(400).json('unable to fetch'));

但我在拨打 URL 时无法获取。

所以请帮忙。 提前致谢。

试试这个:

db('comments AS c')
  .join('post_details AS p', (joinBuilder) => {
    return joinBuilder.on('p.id', '=', 'c.postid').andOn('c.postid', '=', db.raw('?', [postid]));
  })
  .join('users AS u', 'u.id', '=', 'c.userid')
  .select(['c.id', 'c.parent_id', 'c.comment', 'u.username', 'c.postid', 'c.userid'])
  .then((data) => {
    console.log(data);
    res.json(data);
  })
  .catch((err) => res.status(400).json('unable to fetch'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM