如果参数为NULL,如何从WHERE子句中删除条件 [英] How to remove conditions from WHERE clause if parameters are NULL

查看:89
本文介绍了如果参数为NULL,如何从WHERE子句中删除条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将2个参数传递给PL/pgSQL函数.这是查询:

I'm passing 2 parameters to a PL/pgSQL function. Here's the query:

SELECT * 
FROM table 
WHERE col1 = param1 
  AND col2 = param2

两个参数都可以为NULL,在这种情况下,应从WHERE子句中删除相应的表达式.

Both parameters can be NULL, in which case the respective expression should be removed from the WHERE clause.

我该怎么做?符合IF条件?

How can I do that? With IF conditions?

推荐答案

也许是在解决问题:

SELECT * 
FROM table 
WHERE col1 = param1 
  AND (param2 is null or col2 = param2);

这不是删除AND条件,但是在param2为null的情况下应该不重要.因此,不清楚地回答您的问题,而是四处走动...;)

This is not removing the AND condition, but should make the unimportant in case of param2 is null. So not clearly answering your question but going around... ;)

这篇关于如果参数为NULL,如何从WHERE子句中删除条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆