SQL更改每个数据的where子句值通过条件

时间:2019-04-05 23:15:49

标签: sql

我有此功能,我可以在其中计算db中的数据。

例如

Select column1, count(), column2 
from table1 
left join table 2 and so on
where --- some statement
and (table1.date - table2.date) <= 2
group by column1, column2

现在,我需要对要检查的每个数据在where子句中执行类似的操作,如果table2的状态为1,则where子句将为where status = 1,如果没有,则where where status = 2

这是因为我需要的是,如果table2中的状态为1,那就是我将在table2.date中使用的数据,否则,我将状态2的数据用作table2.date

2 个答案:

答案 0 :(得分:1)

  

如果table2的状态为1,那么我的where子句将为where status = 1,但如果不是,则where status = 2

对我来说,这将转换为查询WHERE子句中的以下条件:

(table2.status = 1 AND table1.status = 1) OR table1.status = 2

答案 1 :(得分:0)

假设NULL列中没有status值,则所需的逻辑是:

where (table2.status = 1 and table1.status = 1) or
      (table2.status <> 1 and table1.status = 2)