簡體   English   中英

如何在Sequelize中自我加入?

[英]How can I do self join in Sequelize?

我有兩個atributes一個參與模式: cid談話ID和uid用戶ID。

我有這個查詢:

SELECT tA.cid
FROM participation tA, participation tB
WHERE tA.uid = "aaa" AND tB.uid = "aab" AND tA.cid = tB.cid;

它將返回兩個用戶都參與的會話的ID-只有該會話包括兩個用戶。

編輯:對話模型和用戶模型通過參與模型具有“ 多對多”關聯。

那么如何在Sequelize中進行查詢? 我可以使用include嗎?

participation.findAll({
  include: [{
    model: participation
  }]

  //Other options...
})

如果您通過單獨的模型(具有“有很多”或“屬於很多”關系)定義了ID來加入ID,則將使用包含。 然后,您可以嘗試以下操作:

conversation.findAll({
      where: {
      [Op.and]: [{uid: "aaa"}, {uid: "aab"}]
      include: [{
        model: user,
        through: {
          attributes: ["uid", "cid"]
        }
      }]
    })


Otherwise you could try something like this:

participation.findAll({
  where: {
    [Op.and]: [{uid: "aaa"}, {uid: "aab"}]
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM