2017-12-27 175 views
0
...... 
..... 

return myDb.myTable.rollgroup.findAll({ 

       attributes : ['ID','GROUPNAME'], 
       where : { 
        id : { 
         $notIn: ? //// how to use subQuery here 
        } 
       } 


       }).then((getResult) => { 

        return getResult; 

       }) 
....... 
....... 

我的原始查询像这样:如何正确使用子查询的续集?

从MYGROUP毫克其中mg.ID不

回答

0
(?从employeegrouprelation EGR其中egr.PID =选择egr.GROUPID)选择mg.ID,mg.GROUPNAME

您可以使用sequelize.literal的包裹子查询:

... 

    id:{ 
     $notIn: sequelize.literal('(select egr.GROUPID from employeegrouprelation egr where egr.PID = ?)') 
     } 

    ... 
+0

它的工作,但正确的方式? – mani

+0

另一种方法是先运行子查询以返回一个groupid的数组,然后使用第一个结果将其他查询链接到它上面。所以像'return model.findAll({attributes:['groupid'],其中:{groupid:}})。then((result)=> {next查询使用$ notIn中的结果})''。无论你喜欢什么。 – Faz

0

另一种方式来做到这一点

myDb.myTable.employeegrouprelation.findAll({ 
     attributes: [egr.GROUPID], 
     where: { 
      egr.PID: ???? 
     } 
    }).then((ids) => { 
     var id = [0] 
     ids.map((id) => { 
     id.push(id.GROUPID) 
     }) 
     return myDb.myTable.rollgroup.findAll({ 
      attributes : ['ID','GROUPNAME'], 
      where : { 
       id : { 
       $notIn: id 
       } 
      } 


     }).then((getResult) => { 

      return getResult; 

     }) 
    })