mysql - Sequelize 中的相关子查询和条件 where 子句
我有一个相关子查询,它是基于列值进行验证的。尽管我阅读了 sequelize 文档和 GitHub 上的示例,但我实际上并没有得到任何可行的解决方案。
我正在尝试执行的查询
select count(*) from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
任何参考资料或建议都将不胜感激。
更新:
目标是从 table1 获取所有记录,但如果某条记录的 deciding_col 值为 true,那么我需要检查该特定记录是否存在于 table2 中。
示例:
Table1 数据:
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
表2 数据:
id | second_condition
1 132
输出:1.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
2.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=133)
else 1=1 end;
id | name | deciding_col
2 edfg 0
3 xyz 0