Home > other >  how to write condition for " A or (B and c) " in sequelize Node js
how to write condition for " A or (B and c) " in sequelize Node js

Time:04-14

how do I write where condition for " A or (B and c) "

current where condition is :

    Post.findAll({
  where: {
    Id: {
      [Op.or]: [A, B]
    }
  }
});

but I need OR condition with an "and" in it

like

Post.findAll({
      where: {
        Id: {
          [Op.or]: [A, (B [Op.and] {name : C}) // this is wrong query, what is the right way ? ]
        }
      }
    });

query in mysql be something like

select * from table where id ='A' or (id='B' and name='C');

The above Sql is wrong , but the condition is what i need. Any help .

CodePudding user response:

Because or is a top level operator in the SQL query you need to indicate it as a top level prop in where option as well and use Op.and (or just an object with several props) inside it in the second condition:

Post.findAll({
      where: {
          [Op.or]: [{
            Id: 'A'
          }, {
           Id: 'B',
           Name: 'C'
          }]
      }
    });
  • Related