带有可以为 NULL 的参数的 sql select

发布于 2024-11-06 10:46:54 字数 990 浏览 0 评论 0原文

拥有一个参数可以为 NULL 的选择查询的最佳方法是什么,具体取决于程序中的某些变量?

我可以想到两种解决方案(伪代码):

bool valueIsNull;
int value;

query = "SELECT * FROM table WHERE field ";

if (valueIsNull)
{
  query += "IS NULL";
}
else
{
  query += "= ?";
}
statement st = sql.prepare(query);

if (!valueIsNull)
{
  st.bind(0, value);
}

或者

bool valueIsNull;
int value;

query = "SELECT * FROM table WHERE field = ? OR (? IS NULL AND field IS NULL)";
statement st = sql.prepare(query);
if (valueIsNull)
{
  st.bindNull(0);
  st.bindNull(1);
}
else
{
  st.bind(0, value);
  st.bind(1, value);
}

只是一个简单的 SELECT 语句就有很多代码,我发现它很难看且不清楚。

最干净的方法是这样的:

bool valueIsNull;
int value;

query = "SELECT * FROM table WHERE field = ?";   // <-- this does not work
statement st = sql.prepare(query);
st.bind(0, value, valueIsNull);  // <-- this works

显然这是行不通的。但有没有一种干净的方法来处理这个问题呢?

我认为这并不重要,但我正在使用 C++、cppdb 和 postgresql。

What is the best way to have a select query which has an argument which can be NULL depending on some variable in the program?

I can think of two solutions (pseudocode):

bool valueIsNull;
int value;

query = "SELECT * FROM table WHERE field ";

if (valueIsNull)
{
  query += "IS NULL";
}
else
{
  query += "= ?";
}
statement st = sql.prepare(query);

if (!valueIsNull)
{
  st.bind(0, value);
}

or

bool valueIsNull;
int value;

query = "SELECT * FROM table WHERE field = ? OR (? IS NULL AND field IS NULL)";
statement st = sql.prepare(query);
if (valueIsNull)
{
  st.bindNull(0);
  st.bindNull(1);
}
else
{
  st.bind(0, value);
  st.bind(1, value);
}

It is a lot of code for just a simple SELECT statement and I find it just ugly and unclear.

The cleanest way would be something like:

bool valueIsNull;
int value;

query = "SELECT * FROM table WHERE field = ?";   // <-- this does not work
statement st = sql.prepare(query);
st.bind(0, value, valueIsNull);  // <-- this works

Obviously this does not work. But is there a clean way to handle this?

I do not think it matter much but I am using C++, cppdb and postgresql.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-11-13 10:46:54

使用Postgresql(但我相信不是标准的),您可以使用

SELECT * from some_table where field IS NOT DISTINCT FROM ?;

IS NOT DISTINCT FROM,与普通=不同,当两边都是NULL时为真。

With Postgresql (but I believe not standard) you can use

SELECT * from some_table where field IS NOT DISTINCT FROM ?;

IS NOT DISTINCT FROM, unlike plain =, is true when both sides are NULL.

嘿看小鸭子会跑 2024-11-13 10:46:54

正如您所指出的,主要问题是:

SELECT * FROM table WHERE field = ? OR (? IS NULL AND field IS NULL)

它实际上是需要绑定的两个参数。

您可以使用(应该是相当可移植的)构造来解决这个问题,例如:

SELECT *
FROM table
INNER JOIN (SELECT ? AS param1 /* FROM DUAL */) AS params
    ON 1 = 1
WHERE field = param1
    OR COALESCE(param1, field) IS NULL

As you've noted, the main problem with this:

SELECT * FROM table WHERE field = ? OR (? IS NULL AND field IS NULL)

is that it is actually two parameters which need to be bound.

You can get around that with a (should be fairly portable) construct like:

SELECT *
FROM table
INNER JOIN (SELECT ? AS param1 /* FROM DUAL */) AS params
    ON 1 = 1
WHERE field = param1
    OR COALESCE(param1, field) IS NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文