2020年5月27日 星期三

Sequelize 作subquery 範例

orm作單一table查詢 若需要查詢的條件需引用自子查詢
以下是簡單的範例
sql作法

select * from maintable where uid in(select uid from uidTable where flag='Y')
以sequelize model執行方式
mainTableModel.findAll(
  {
    where:{
      uid: {
        [Op.in]: [sequelize.literal(`(select name, uid from maintable where uid in(select uid from uidTable where flag='Y')`)]
      }
    }
  }
)

沒有留言: