我在数据库中有4个表
Users
列Id, Email, Password, Active
InformationData
列Id, Title, Description, Link
Filters
列Id, Keyword, Active
LUserToFilter
列UserId, FilterId
现在我想要的是从InformationData
表中过滤特定记录,其中标题和描述不包含过滤器表中的关键字,我们根据特定用户ID从LUserToFilter
表中选择那些过滤器。
我尝试了这个查询,但它只返回包含过滤关键字的记录,而不返回其他记录。
Create Procedure [dbo].[SP_GetFilteredData]
@userid int
as
begin
select D.* from Filters f
inner join LUsersToFilters L on L.FilterId=F.Id
inner join InformationData D on D.Title like '%'+F.KeyWord+'%' or D.Description like '%'+F.KeyWord+'%'
where L.UserId=@userid
end
请提出任何建议..
答案 0 :(得分:1)
SELECT D.* FROM InformationData D
WHERE NOT EXISTS
(SELECT * FROM Filters F INNER JOIN LUsersToFilters L ON F.id=L.FilterId
WHERE (D.Title LIKE '%'+F.KeyWord+'%' OR D.Description LIKE '%'+F.KeyWord+'%')
AND L.userid = @userid)
答案 1 :(得分:0)
Create Procedure [dbo].[SP_GetFilteredData]
@userid int
as
begin
select D.* from Filters f
inner join LUsersToFilters L on L.FilterId=F.Id
inner join InformationData D on NOT (D.Title like '%'+F.KeyWord+'%') AND NOT (D.Description like '%'+F.KeyWord+'%')
where L.UserId=@userid
end