Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 由非';无法阻止sql server_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql server 由非';无法阻止sql server

Sql server 由非';无法阻止sql server,sql-server,sql-server-2008,Sql Server,Sql Server 2008,我目前正在我的web应用程序中实现阻塞功能。用户可以选择阻止其他用户 我当前的表格设置如下: CREATE TABLE [User].[Block_List] ( [Id] [bigint] IDENTITY(1,1) NOT NULL, [UserId] [bigint] NOT NULL, -- Person that I have blocked [BlockedById] [bigint] NOT NULL, -- Person who requested the

我目前正在我的web应用程序中实现阻塞功能。用户可以选择阻止其他用户

我当前的表格设置如下:

CREATE TABLE [User].[Block_List]
(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [UserId] [bigint] NOT NULL, -- Person that I have blocked
    [BlockedById] [bigint] NOT NULL, -- Person who requested the block
    [DateBlocked] [datetime] NOT NULL,
    [DateUnblocked] [datetime] NULL
)
现在,在我的存储过程中,我加入了这个表,以筛选出我已阻止的人员,如下所示:

 SELECT 
     p.Id [SenderId], 
     p.Username, 
     COUNT(mr.RecipientId) [TotalMessages], 
     COUNT(CASE WHEN mr.ReadDate IS NULL THEN 1 END) AS [NewMessages],
     up.PhotoId, 
     p.LastLoggedIn, 
     p.LoggedIn,
     MAX(m.SentDate)[EmailDate]
 FROM 
     [User].[User_Profile] p
 JOIN 
     [MailBox].[Message] m ON p.Id = m.SenderId
 JOIN 
     [MailBox].[MessageRecipient] mr ON m.Id = mr.MessageId
 LEFT JOIN 
     [User].[User_Photos] up ON p.Id = up.UserId AND up.isProfilePic = 1
 LEFT JOIN 
     [User].[Block_List] b ON p.Id = b.UserId
 WHERE 
     mr.RecipientId = 1 --and mr.DeletedDate is null
     AND (b.BlockedById = 1 and p.Id != b.UserId) -- Checking blocked table
 GROUP BY 
     p.id, p.Username, mr.RecipientId, up.PhotoId, p.LastLoggedIn, p.LoggedIn
我已将
BlockedbyId
硬编码为1,因为这是我当前的Id,不幸的是,当我希望它返回至少一行时,它不会返回任何行,原因是我有两条记录,我只阻止了其中一条

当我在没有阻塞where子句的情况下执行上述存储过程时,我得到以下结果:

在我的阻止表中,我已阻止Id为2的用户,如下所示:

 SELECT 
     p.Id [SenderId], 
     p.Username, 
     COUNT(mr.RecipientId) [TotalMessages], 
     COUNT(CASE WHEN mr.ReadDate IS NULL THEN 1 END) AS [NewMessages],
     up.PhotoId, 
     p.LastLoggedIn, 
     p.LoggedIn,
     MAX(m.SentDate)[EmailDate]
 FROM 
     [User].[User_Profile] p
 JOIN 
     [MailBox].[Message] m ON p.Id = m.SenderId
 JOIN 
     [MailBox].[MessageRecipient] mr ON m.Id = mr.MessageId
 LEFT JOIN 
     [User].[User_Photos] up ON p.Id = up.UserId AND up.isProfilePic = 1
 LEFT JOIN 
     [User].[Block_List] b ON p.Id = b.UserId
 WHERE 
     mr.RecipientId = 1 --and mr.DeletedDate is null
     AND (b.BlockedById = 1 and p.Id != b.UserId) -- Checking blocked table
 GROUP BY 
     p.id, p.Username, mr.RecipientId, up.PhotoId, p.LastLoggedIn, p.LoggedIn

当我添加blockingwhere子句时,我希望看到userid21的记录,但不幸的是,我没有得到任何返回的记录


任何帮助都很好

这里设置了一些相互排斥的条件。你的加入说明

p.Id = b.UserId
然而where条款也说

p.Id != b.UserId
因此,当您合并它们时,将不会得到任何行。

以下部分

LEFT JOIN [User].[Block_List] b on p.Id = b.UserId
WHERE mr.RecipientId = 1 --and mr.DeletedDate is null
AND (b.BlockedById = 1 and p.Id != b.UserId) -- Checking blocked table
应该由

WHERE mr.RecipientId = 1
AND m.SenderId NOT IN (SELECT UserId FROM [User].[Block_List] WHERE BlockedById = 1)

特别是如果你想数数,我在加入一个表时会非常小心,因为如果之后没有正确地过滤掉,你可能会有太多的组合,在我看来,在可能的情况下使用where子句更安全,除非您真的想加入并使用这些值。

您的加入表示
p.Id=b.UserId
,但where子句也表示
p.Id!=b、 UserId
@maraca我使用的不存在纯粹是因为此sp需要满足我阻止某人的需要,反之亦然