我有四种不同的情况。
我从外面得到2个值(A,B)。
如果A和B都为空
我想先选择条件下的行。
如果A为空且B不为,则第二个为条件。
如果A不为空且B为空,则为第三个。
如果A和B都不为空,则为第四个。
让我简要介绍一下。
select * from mytable
if(A ==null and B == null)
first where condition
if(A == null and B != null)
second where condition
......
如何使用SQL
执行此操作答案 0 :(得分:2)
也许你可以使用case子句,我认为这对你来说会更容易。 这是您的问题的示例,我希望此代码可以帮助您。
SELECT * FROM mytable
WHERE name =
CASE WHEN A IS NULL AND B IS NULL THEN 'john'
WHEN A IS NULL AND B IS NOT NULL THEN 'doe'
...
END;
答案 1 :(得分:0)
您想要满足的四种不同条件:
这些可以这样表达:
WHERE (A IS NULL AND B IS NULL AND NAME = 'Fred')
OR (A IS NULL AND B IS NOT NULL AND NAME = 'Barney')
OR (A IS NOT NULL AND B IS NULL AND NAME = 'Wilma')
OR (A IS NOT NULL AND B IS NOT NULL AND NAME = 'Betty')