Answers:
SELECT CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END AS [Value]
FROM Sites S
WHERE S.Id = @SiteId and S.Status = 1 AND
(S.WebUserId = @WebUserId OR S.AllowUploads = 1)
这类似于Adam Robinson的,但是使用ISNULL而不是COUNT。
SELECT ISNULL(
(SELECT 1 FROM Sites S
WHERE S.Id = @SiteId and S.Status = 1 AND
(S.WebUserId = @WebUserId OR S.AllowUploads = 1)), 0)
如果内部查询具有匹配的行,则返回1。然后,外部查询(带有ISNULL)返回此值1。如果内部查询没有匹配的行,则不返回任何内容。外部查询将其视为NULL,因此ISNULL最终返回0。
这可能是一匹老马,当不存在任何行时返回1行的另一种方式是对UNION进行另一个查询并在表中不存在时显示结果。
SELECT S.Status, COUNT(s.id) AS StatusCount
FROM Sites S
WHERE S.Id = @SiteId
GROUP BY s.Status
UNION ALL --UNION BACK ON TABLE WITH NOT EXISTS
SELECT 'N/A' AS Status, 0 AS StatusCount
WHERE NOT EXISTS (SELECT 1
FROM Sites S
WHERE S.Id = @SiteId
)
SELECT 0
)的查询进行了联合,然后SUM
对联合做了a 。简单易懂。
我在这里阅读了所有答案,花了一段时间才弄清楚发生了什么。以下是根据Moe Sisko的回答和一些相关研究得出的
如果您的SQL查询不返回任何数据,则不存在具有null值的字段,因此ISNULL和COALESCE均无法按您希望的那样工作。通过使用子查询,顶级查询将获得一个具有空值的字段,并且ISNULL和COALESCE均可按您希望/期望的方式工作。
我的查询
select isnull(
(select ASSIGNMENTM1.NAME
from dbo.ASSIGNMENTM1
where ASSIGNMENTM1.NAME = ?)
, 'Nothing Found') as 'ASSIGNMENTM1.NAME'
我的查询与评论
select isnull(
--sub query either returns a value or returns nothing (no value)
(select ASSIGNMENTM1.NAME
from dbo.ASSIGNMENTM1
where ASSIGNMENTM1.NAME = ?)
--If there is a value it is displayed
--If no value, it is perceived as a field with a null value,
--so the isnull function can give the desired results
, 'Nothing Found') as 'ASSIGNMENTM1.NAME'
您只需要用LEFT JOIN替换WHERE:
SELECT CASE
WHEN S.Id IS NOT NULL AND S.Status = 1 AND ...) THEN 1
ELSE 0
END AS [Value]
FROM (SELECT @SiteId AS Id) R
LEFT JOIN Sites S ON S.Id = R.Id
此解决方案使您还可以返回每列的默认值,例如:
SELECT
CASE WHEN S.Id IS NULL THEN 0 ELSE S.Col1 END AS Col1,
S.Col2,
ISNULL(S.Col3, 0) AS Col3
FROM
(SELECT @Id AS Id) R
LEFT JOIN Sites S ON S.Id = R.Id AND S.Status = 1 AND ...
没有匹配的记录意味着没有记录返回。如果未找到任何记录,则“ 0”的值无处可走。您可以创建一个疯狂的UNION查询来执行所需的操作,但是只需检查结果集中的记录数,就可以做得更好。
这可能是一种方式。
SELECT TOP 1 [Column Name] FROM (SELECT [Column Name] FROM [table]
WHERE [conditions]
UNION ALL
SELECT 0 )A ORDER BY [Column Name] DESC
带领带呢?
SELECT TOP 1 WITH TIES tbl1.* FROM
(SELECT CASE WHEN S.Id IS NOT NULL AND S.Status = 1
AND (S.WebUserId = @WebUserId OR
S.AllowUploads = 1)
THEN 1
ELSE 0 AS [Value]
FROM Sites S
WHERE S.Id = @SiteId) as tbl1
ORDER BY tbl1.[Value]
@ hai-phan的答案使用LEFT JOIN
是关键,但是可能很难遵循。我有一个复杂的查询,也可能不返回任何内容。我只是简化了他对我的需求的回答。应用于具有许多列的查询很容易。
;WITH CTE AS (
-- SELECT S.Id, ...
-- FROM Sites S WHERE Id = @SiteId
-- EXCEPT SOME CONDITION.
-- Whatever your query is
)
SELECT CTE.* -- If you want something else instead of NULL, use COALESCE.
FROM (SELECT @SiteId AS ID) R
LEFT JOIN CTE ON CTE.Id = R.ID
更新:SqlServerCentral上的此答案是最好的。它利用了MAX的这一特性- “当没有可供选择的行时,MAX返回NULL”。
SELECT ISNULL(MAX(value), 0) FROM table WHERE Id = @SiteId
You should avoid using expensive methods. You dont need any column for TBL2.
SELECT COUNT(*) FROM(
SELECT TOP 1 1 AS CNT FROM TBL1
WHERE ColumnValue ='FooDoo' ) AS TBL2
or
IF EXISTS (SELECT TOP 1 1 FROM TABLE1 AS T1
WHERE T1.ColumnValue='VooDoo')
SELECT 1
ELSE
SELECT 0