许多条件取决于 WHERE 子句中的 IF 子句
Many conditions depends on IF clause within WHERE clause
如何实现依赖于一个 @value
条件的 WHERE
子句,如下面的伪代码:
Select * from table
WHERE
IF(@value is not null)
Id > 10 and Name = 'example' and Address is not null and ... etc
ELSE
Email is not null
如您所见,您不能像那样使用 if
,但您可以使用 and
和 or
逻辑运算符创建所需的行为:
SELECT *
FROM table
WHERE (@value IS NOT NULL AND
id > 10 AND
name = 'example' AND
address IS NOT NULL AND -- etc...) OR
(@value IS NULL AND email IS NOT NULL)
只有当 @value
为 null
时,您才希望 return 记录 email
不为空。
您需要在示例中为“if .. else”的两个“分支”指定“前提条件”。
SELECT *
FROM Table
WHERE (@value IS NOT NULL AND Id > 10 AND ....)
OR (@value IS NULL AND Email IS NOT NULL)
如何实现依赖于一个 @value
条件的 WHERE
子句,如下面的伪代码:
Select * from table
WHERE
IF(@value is not null)
Id > 10 and Name = 'example' and Address is not null and ... etc
ELSE
Email is not null
如您所见,您不能像那样使用 if
,但您可以使用 and
和 or
逻辑运算符创建所需的行为:
SELECT *
FROM table
WHERE (@value IS NOT NULL AND
id > 10 AND
name = 'example' AND
address IS NOT NULL AND -- etc...) OR
(@value IS NULL AND email IS NOT NULL)
只有当 @value
为 null
时,您才希望 return 记录 email
不为空。
您需要在示例中为“if .. else”的两个“分支”指定“前提条件”。
SELECT *
FROM Table
WHERE (@value IS NOT NULL AND Id > 10 AND ....)
OR (@value IS NULL AND Email IS NOT NULL)