Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

50
Views
Secuela de la construcción dinámica donde la condición de la cláusula con la cláusula AND y OR

Estoy tratando de construir una dinámica de secuela donde la condición de la cláusula tenga una cláusula AND y OR.

 const objArr = [{ active: 'true' }]; for (let i = 0; i < dynamicList.length; i++) { objArr.push({ [Op.or]: [{ hierarchy: { [Op.like]: `%${dynamicList[i]}%` } }, { id: dynamicList[i] }], }); } const condition = { where: { [Op.and]: objArr, }, attributes: [Sequelize.fn('DISTINCT', Sequelize.col('name'))], }; db.tableName .findAll(condition) .then((resul) => { callback(null, result); }) .catch((err) => { callback(err, null); });

Consulta SQL generada

 SELECT DISTINCT(name) FROM tableName AS tableName WHERE (tableName.active = 'true' AND (tableName.hierarchy LIKE '%111%' OR tableName.id = '111') AND (tableName.hierarchy LIKE '%222%' OR tableName.id = '222'));

Consulta SQL esperada --> active=true AND ((dynamic_list) OR (dynamic_list))

 SELECT DISTINCT(name) FROM tableName AS tableName WHERE (tableName.active = 'true' AND (tableName.hierarchy LIKE '%111%' OR tableName.id = '111') OR (tableName.hierarchy LIKE '%222%' OR tableName.id = '222'));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe combinar todas las condiciones dinámicas en un Op.or superior y solo luego agregar este Op.or a una matriz para Op.and :

 const objArr = [{ active: 'true' }]; const dynamicConditions = []; for (let i = 0; i < dynamicList.length; i++) { dynamicConditions.push({ [Op.or]: [{ hierarchy: { [Op.like]: `%${dynamicList[i]}%` } }, { id: dynamicList[i] }], }); } if (dynamicConditions.length) { objArr.push({ [Op.or]: dynamicConditions }); } const condition = { where: { [Op.and]: objArr, }, attributes: [Sequelize.fn('DISTINCT', Sequelize.col('name'))], };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!