我有编写StackExchange DataExplorer查询,以列出User.Id的所有注释,查询工作,并返回questions和answers的Ids。我不明白的是,对于answers,为什么第二列是空的。
DECLARE @UserId int = ##UserId##
Select p.Id
, '<a href=https://stackoverflow.com/questions/'
+ Cast(p.Id as varchar(20)) + '>'
+ Cast(p.Id as varchar(20))
+ ' - ' + p.Title + '</a>'
, c.Text
FROM Users u
Join Comments c ON c.UserId = @UserId
JOIN Posts p ON p.Id = c.PostId
where u.Id = @UserId AND p.Id IS NOT NULL即使假设列p.Title为空,列p.Id也不为空,因此我希望这一部分
'<a href=https://stackoverflow.com/questions/'
+ Cast(p.Id as varchar(20)) + '>'
+ Cast(p.Id as varchar(20))
+ ' - ' + p.Title + '</a>'会按照这个问题返回一些内容。但第二栏完全是空的。
为什么是这种情况?
发布于 2014-02-26 19:13:32
即使假设列p.Title为空
这是为了那些行。
列p.Id不是空的,因此我希望结果是非空的。
不是的。如果使用NULL操作符将+与Server中的任何内容连接起来,则最终得到NULL,除非concat_null_yields_null是OFF。
您可以使用CONCAT函数代替。这也节省了对CAST的需求。
DECLARE @UserId INT = ##UserId##
SELECT p.Id,
CONCAT('<a href=http://stackoverflow.com/questions/',
p.Id,
'>',
p.Id,
' - ',
p.Title COLLATE SQL_Latin1_General_CP1_CI_AS,
'</a>'),
c.Text
FROM Users u
JOIN Comments c
ON c.UserId = @UserId
JOIN Posts p
ON p.Id = c.PostId
WHERE u.Id = @UserId
AND p.Id IS NOT NULL https://stackoverflow.com/questions/22050355
复制相似问题