是否可以在WHERE子句中使用IF ELSE ...
WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
IF(@transactionType='1')
AND qTable.statusId like 'SIGNED'
ELSE
AND rTable.statusId like 'ACTIVE'
答案 0 :(得分:2)
您不能在IF ELSE
子句中使用WHERE
语句。请改用CASE
:
WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
AND CASE WHEN @transactionType = '1' THEN qTable.statusId like 'SIGNED'
ELSE rTable.statusId like 'ACTIVE' END
答案 1 :(得分:1)
WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
AND
((@transactionType='1' AND qTable.statusId like 'SIGNED')
OR
(@transactionType <> '1' AND like 'ACTIVE') )
答案 2 :(得分:0)
这是你需要的吗?
WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
AND ((transactionType='1' and qTable.statusId like 'SIGNED') or (transactionType <> '1' and rTable.statusId like 'ACTIVE'))