当目标表也与第三个表关联时如何在连接中添加条件

发布于 2025-01-11 05:05:53 字数 317 浏览 0 评论 0原文

假设我们有 表A, 表B, 表C,

a通过id与b关联 a 通过键与 c 关联 b 通过移动设备与 c 关联

,现在我在续集中查询,

A.findAll({
   include: [
     {
       model: b
       include: [
          {
            model: c
          }
       ]
     }
   ]
})

如何在 ON 中加入 B 和 C 时包含此条件(B.mobile = C.mobile 和 C.key = A.key)

Let's say we have
table A,
table B,
table C,

a is associated with b by id
a is associated with c by key
b is associated with c by mobile

now i am querying in sequelize

A.findAll({
   include: [
     {
       model: b
       include: [
          {
            model: c
          }
       ]
     }
   ]
})

now how can i include this condition on joining B and C in ON (B.mobile = C.mobile and C.key = A.key)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

峩卟喜欢 2025-01-18 05:05:53

您可以使用 include 选项的 on 选项,如下所示:

const items = A.findAll({
  include: [
    model: B,
    include: [{
      model: C,
      on: {
        mobile: Sequelize.col('B.mobile'),
        key: Sequelize.col('A.key'),
      }
    }]
  ]
}

如果这样的 ON 在 MySQL 中不起作用,那么您可以尝试移动 >key: Sequelize.col('A.key') 到主 where 选项:

const items = A.findAll({
  where: {
    key: Sequelize.col('C.key')
  },
  include: [
    model: B,
    include: [{
      model: C,
      on: {
        mobile: Sequelize.col('B.mobile'),
      }
    }]
  ]
}

You can use on option of the include option like this:

const items = A.findAll({
  include: [
    model: B,
    include: [{
      model: C,
      on: {
        mobile: Sequelize.col('B.mobile'),
        key: Sequelize.col('A.key'),
      }
    }]
  ]
}

If such ON wouldn't work in MySQL then you can try to move key: Sequelize.col('A.key') to the main where option:

const items = A.findAll({
  where: {
    key: Sequelize.col('C.key')
  },
  include: [
    model: B,
    include: [{
      model: C,
      on: {
        mobile: Sequelize.col('B.mobile'),
      }
    }]
  ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文