如果没有找到记录则返回一个值

发布于 2024-12-15 04:57:37 字数 194 浏览 1 评论 0原文

我有一个有效的简单语句:

SELECT idnumber FROM dbo.database WHERE number = '9823474'

如果该数字不存在于表中的任何位置,则失败。我想在此声明中添加一些内容:
如果没有找到记录,则返回 NULL 而不是没有行。

有什么建议吗?

I have this simple statement that works:

SELECT idnumber FROM dbo.database WHERE number = '9823474'

If the number does not exist anywhere in the table, it fails. I would like to add something to this statement that says:
IF NO RECORD IS FOUND RETURN NULL INSTEAD OF NO ROW.

Any suggestions?

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

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

发布评论

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

评论(7

掩于岁月 2024-12-22 04:57:37

将查询封装在子查询中,将“无行”转换为 null 值。

我使用 PostgreSQL 对此进行了测试和验证, SQLiteSQL ServerMySQL

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id;

Oracle您必须从虚拟 1 行表 DUAL 中进行选择:

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id FROM DUAL;

可以在 MySQL 中执行相同的操作以实现兼容性原因,但你不必这样做。
类似 Firebird< /a>:

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id FROM RDB$DATABASE;

这对DB2(就像肖恩评论):

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id FROM SYSIBM.SYSDUMMY1;

Encapsulate the query in a sub-query to transform "no row" to a null value.

I tested and verified this with PostgreSQL, SQLite, SQL Server, and MySQL.

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id;

In Oracle you have to select from the dummy 1-row table DUAL:

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id FROM DUAL;

You can do the same in MySQL for compatibility reasons, but you don't have to.
Similar in Firebird:

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id FROM RDB$DATABASE;

This does it for DB2 (like Sean commented):

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id FROM SYSIBM.SYSDUMMY1;
弥繁 2024-12-22 04:57:37

为了使其更简单,这应该可以正常工作。如果您根据 idnumber 的数据类型将其分配给变量,那么您将能够评估该值是 null 还是实际的 idnumber 返回。

SELECT ISNULL(
      (
         SELECT idnumber 
         FROM dbo.database 
         WHERE number = '9823474'
      ), NULL)

To make it more simplier, this should work fine. If you assign this to a variable based on the datatype of your idnumber than you would be able to evaluate whether the value is null or the actual idnumber return.

SELECT ISNULL(
      (
         SELECT idnumber 
         FROM dbo.database 
         WHERE number = '9823474'
      ), NULL)
花想c 2024-12-22 04:57:37
Select isnull(sum(Amount),0) as Amt from BeginningBalance where CustomerID = @CustomerID
Union all
Select isnull(sum(Amount),0) as Amt from SalesOrders where CustomerID = @CustomerID
Union all
Select isnull(sum(Amount),0) as Amt from SalesInvoices where CustomerID = @CustomerID
//Data Row Result if no data is present at Beginning Balance Table
// example 

Amt
2000  // amount from sales orders
1000  // amount from sales invoices

// if the 1st select statement return no data use this
SELECT (select sum(Amount) from BeginningBalance 
        where CustomerID = @CustomerID) as Amt
Union all
Select sum(Amount) as Amt from SalesOrders where CustomerID = @CustomerID
Union all
Select sum(Amount) as Amt from SalesInvoices where CustomerID = @CustomerID

结果 :

Amt
NULL  // amount from BeginningBalance
2000  // amount from sales orders
1000  // amount from sales invoices
Select isnull(sum(Amount),0) as Amt from BeginningBalance where CustomerID = @CustomerID
Union all
Select isnull(sum(Amount),0) as Amt from SalesOrders where CustomerID = @CustomerID
Union all
Select isnull(sum(Amount),0) as Amt from SalesInvoices where CustomerID = @CustomerID
//Data Row Result if no data is present at Beginning Balance Table
// example 

Amt
2000  // amount from sales orders
1000  // amount from sales invoices

// if the 1st select statement return no data use this
SELECT (select sum(Amount) from BeginningBalance 
        where CustomerID = @CustomerID) as Amt
Union all
Select sum(Amount) as Amt from SalesOrders where CustomerID = @CustomerID
Union all
Select sum(Amount) as Amt from SalesInvoices where CustomerID = @CustomerID

Result :

Amt
NULL  // amount from BeginningBalance
2000  // amount from sales orders
1000  // amount from sales invoices
我喜欢麦丽素 2024-12-22 04:57:37

我将它用于 MySql

SELECT IFNULL(ColumnA,"1") AS ColumnA , COUNT(1) AS Total FROM table 
WHERE ID = 1 LIMIT 0, 1;

I use this for MySql

SELECT IFNULL(ColumnA,"1") AS ColumnA , COUNT(1) AS Total FROM table 
WHERE ID = 1 LIMIT 0, 1;
自由如风 2024-12-22 04:57:37

对我来说最简单的方法是使用古老的 IF THEN ELSE 技巧!适用于所有 SQL 风格。

IF EXISTS (SELECT * FROM dbItem WHERE price >= 10)
BEGIN
    SELECT * FROM dbItem WHERE price >= 10
END
ELSE
  SELECT 'No record'

Simplest way for me was to use the good old IF THEN ELSE trick! Works with all SQL flavors.

IF EXISTS (SELECT * FROM dbItem WHERE price >= 10)
BEGIN
    SELECT * FROM dbItem WHERE price >= 10
END
ELSE
  SELECT 'No record'
娇妻 2024-12-22 04:57:37

可以用 COALESCE

COALESCE((SELECT idnumber FROM dbo.database WHERE number = '9823474'),0)

替换 0 与您希望在选择查询不返回任何行时返回的任何值。

You can use COALESCE

COALESCE((SELECT idnumber FROM dbo.database WHERE number = '9823474'),0)

Replace 0 with any value that you would like to return if the select query returns no rows.

小鸟爱天空丶 2024-12-22 04:57:37

这是 Oracle 中的一个独立概念验证,它返回实际值而不是 NULL

在 Oracle 中,“双”表始终有一个名为“dummy”的列,其中包含“X”。因此,以下语句永远不会返回行。

select * from dual where dummy='123';

因此,由于 nvl 函数,该语句将始终返回“NO RECORD FOUND”,

select nvl((select * from dual where dummy='123'),'NO RECORD FOUND')
value 
from dual;

但是,如果您确实想要 NULL,则可以执行此操作(如上所述)

select (select * from dual where dummy='123') value from dual;

当然,请将上面的 select 语句与您自己的语句交换

Here is a standalone proof-of-concept in Oracle, which returns a real value instead of NULL

In Oracle, the "dual" table always has a column called "dummy" which contains 'X'. Therefore, the following statement will never return a row.

select * from dual where dummy='123';

So this statement will always return "NO RECORD FOUND" thanks to the nvl function

select nvl((select * from dual where dummy='123'),'NO RECORD FOUND')
value 
from dual;

but, if you really want NULL you can do this (as described above)

select (select * from dual where dummy='123') value from dual;

Of course, swap the above select statement with your own

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文