【问题标题】:Search filter on two fields based upon condition根据条件在两个字段上搜索过滤器
【发布时间】:2014-09-03 11:39:38
【问题描述】:

这是我的桌子

create table Table1 (Id int, ...some fields... , CategoryId int, ProfileId int) 

我想编写一个 SP(存储过程),它会根据传递给 SP 的参数为我提供表中的搜索结果。这是我的程序

Create proc Search
(
  @MediaType1 varchar(1000),
  @MediaType2 varchar(1000),
  @MediaType3 varchar(1000)
)

as

begin

  select * from table
  where 
  case when @MediaType1 = '' then 1 else CategoryId end in 
    (select case when @MediaType1 = '' then 1 else Splvalue end 
        from dbo.Split(case @MediaType1 when '0,' then '1,2,3,4' when '' then '1,' else @MediaType1 end,','))
    and
    case when @MediaType2 = '' then 1 else ProfileId end in
    (select case when @MediaType2 = '' then 1 else Splvalue end 
        from dbo.Split(case @MediaType2 when '0,' then '2,12,13' when '' then '1,' else @MediaType2 end,','))
    and
    case when @MediaType3 = '' then 1 else ProfileId end in 
    (select case when @MediaType3 = '' then 1 else Splvalue end 
        from dbo.Split(case @MediaType3 when '0,' then '1,14,15,16' when '' then '1,' else @MediaType3 end,','))

end

基本上,我想要实现的是当在@MediaType1 变量中传递“0”时,它应该返回所有具有类别 (1,2,3,4) 的记录,否则它应该只返回传递的类别(例如3)否则,如果它为空白,则应显示所有记录。 @MediaType2 和 @MediaType3 的方法相同,只是它们应该检查 ProfileId。条件也是所有三个或两个或一个参数都可以为空,我需要处理这些并显示过滤的记录。

我上面的查询只有在一个参数被传递并且其余的都是空白的情况下才有效。我也试过了

where
(@MediaType1 <> '' and Category in (select Splvalue from dbo.Split(@MediaType1,',')))
or
(@MediaType2 <> '' and ProfileId in (select Splvalue from dbo.Split(@MediaType2 ,',')))
or
(@MediaType3 <> '' and ProfileId in (select Splvalue from dbo.Split(@MediaType3 ,',')))

但即使这样也行不通。任何帮助将不胜感激

【问题讨论】:

    标签: sql sql-server stored-procedures sql-server-2012


    【解决方案1】:

    如果我只是将你所说的重写为条件:

    SELECT * FROM table
    WHERE 
     (@MediaType1 = '' OR (@MediaType1 = 0 AND CategoryId IN (1,2,3,4)) 
      OR @MediaType1 = CategoryId)
     AND
      (@MediaType2 = '' OR (@MediaType2 = 0 AND ProfileId IN (1,2,3,4)) 
       OR @MediaType2 = ProfileId )
     AND
      (@MediaType3 = '' OR (@MediaType3 = 0 AND ProfileId IN (1,2,3,4)) 
       OR @MediaType3 = ProfileId )
    

    【讨论】:

    • 对不起,伙计,这适用于单个参数,但不适用于多个参数,我在 MediaType1 中传递了 4,它给了我 4 类记录,我在 MediaType3 中传递了 16,它给了我 16,但是当我同时传递两个时,它给了我空白
    • 您想对每个 mediaType 使用 OR 而不是 AND 吗?更新了答案。
    • 好吧 OR 刚刚打开了所有内容,现在过滤器不起作用,所有内容每次都可见
    • 那么如果将 4 传递给 MediaType1 并将 16 传递给 MediaType3 会发生什么?
    • 它应该显示 CategoryId = 4 的所有记录以及 Profileid = 16 的记录
    【解决方案2】:

    我刚刚这样写了 SP 并且成功了

    Create proc Search
    (
      @MediaType1 varchar(1000),
      @MediaType2 varchar(1000),
      @MediaType3 varchar(1000)
    )
    
    as
    
    begin
    
      if (@MediaType1 = '' and @MediaType2 = '' and @MediaType3 = '')
      begin 
        set @MediaType1 = '0,';
        set @MediaType2 =  '0,';
        set @MediaType3 =  '0,';
      end 
    
      select * from table
      where 
      ((@MediaType1 = '0,' and CategoryId in (1,2,3,4)) or (CategoryId in (select Splvalue from dbo.Split(@MediaType1,','))))
        or
        ((@MediaType2 = '0,' and ProfileId in (2,12,13)) or (ProfileId in (select Splvalue from dbo.Split(@MediaType2 ,','))))
        or
        ((@MediaType3 = '0,' and ProfileId in (1,14,15,16)) or (ProfileId in (select Splvalue from dbo.Split(@MediaType3 ,','))))
    
    end
    

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多