Mysql查询:过滤条件

时间:2016-02-11 21:18:33

标签: php mysql sql filter

我有一个表订单(OID,OrderType,ODep,ReqUser)

我想过滤如下逻辑:

SELECT * FROM Orders if  (Ordertype='confidential') filter by ReqUser='$UserID'  else filter by ODep

我不确定是否可能!

1 个答案:

答案 0 :(得分:1)

在这些情况下,您通常可以将if a then b else c替换为(a AND b) OR ((NOT a) AND c),如下所示:

SELECT * 
FROM Orders 
WHERE (Ordertype='confidential' AND ReqUser='$UserID')
   OR (Ordertype<>'confidential' AND ODep)
;

或者,根据您的数据,这可能也有效:

SELECT * 
FROM Orders 
WHERE ReqUser='$UserID'
   OR (Ordertype<>'confidential' AND ODep)
;

或者这个:

SELECT * 
FROM Orders 
WHERE ODep AND (ReqUser='$UserID' OR Ordertype <> 'confidential')
;