0

假设我有一个这样的 sql:

select * from tba

如果表 tba 有数据,它将如下所示:

id    name    num
1     tom     100
2     jack    200

现在我需要通过 where 条件查询 tba

select num from tba where id = 3;

这是问题:这个sql不再有数据了,会是这样的:

id    name    num
//no data, just have column name

所以,我想告诉我没有结果或只有零,例如:

num
0

我怎样才能得到结果?

谢谢!!

4

2 回答 2

3

试试这个:

SELECT COALESCE(MAX(num), 0) AS num
FROM   tba
WHERE  id = 3

如果要选择所有列,可以执行以下操作:

SELECT COALESCE(MAX(id), 0)       AS id,
       COALESCE(MAX(name), 'N/A') AS name,
       COALESCE(MAX(num), 0)      AS num
FROM   tba
WHERE  id = 3;

SQLFiddle 演示

于 2012-08-01T06:59:31.850 回答
0
SELECT ISNULL(num,0) AS num
  FROM tba
 WHERE id = 3
于 2012-08-01T07:01:54.097 回答