SQL - 如果条件为真,则仅执行'和'

时间:2015-06-02 10:46:50

标签: sql sql-server sql-server-2008 mysqli

如果条件为真,我只希望执行'和'条件。如果为false,则应忽略“和”条件。

select *

from table1 t1
left join table2 t2 on t1.a = t2.a

where t2.a = 'some value'
and t2.b = 'some value' --execute only if f.e. t1.c = 'some value'

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

SELECT *
FROM table1 AS t1
LEFT JOIN table2 t2 ON t1.a = t2.a
WHERE t2.a = 'some value'
  AND ((t2.b = 'some value' AND t1.c = 'some value') OR t1.c <> 'some value')