带有IF子句的SQL Where子句

问题描述 投票:0回答:1

一个问题的问题SQL: IF clause within WHERE clause

让我们说:DeptID是传递给SQL Querry的参数,如果@DeptID为null,则显示全部,否则用DeptID过滤结果。

类似于某些伪代码

SELECT * FROM EMPLOYEE 
IF :DeptID not = null 
   WHERE DeptID = :DeptID
ELSE (no filter)

 Looking for correct syntax suitable for Poastgress & MySQL 
sql postgresql
1个回答
0
投票
SELECT * 
FROM EMPLOYEE 
WHERE :DeptID is null 
   OR :DeptID = DeptID
© www.soinside.com 2019 - 2024. All rights reserved.