【发布时间】:2013-12-13 05:31:11
【问题描述】:
我在数据库中有 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
请给点建议..
【问题讨论】:
-
旁注:您应该不为您的存储过程使用
sp_前缀。微软有reserved that prefix for its own use (see Naming Stored Procedures),你确实会在未来某个时候冒着名称冲突的风险。 It's also bad for your stored procedure performance。最好只是简单地避免sp_并使用其他东西作为前缀 - 或者根本不使用前缀! -
好吧抱歉使用 sp_ 和英文写作,但以这种方式写英文是我的习惯,我会努力改掉它
-
我仍然不完全清楚你想要过滤掉什么(例如不显示),以及你想要显示哪些行。我是否理解正确:您想在标题或描述列中显示来自
InformationData的那些不包含来自Filters的任何字词的行? -
我需要的正是你所理解的......
标签: sql database sql-server-2008 join