在IN和Coalesce的条款

时间:2016-11-28 23:24:51

标签: sql sql-server

说我有一张桌子,音乐:

Genre          Artist              Song
Rock           Rolling Stones      Brown Sugar 
Rock           Bob Seger           Night Moves
Country        Eric Church         Record Year
Pop            Bruno Mars          Grenade

我有

DECLARE @Genre VarChar(MAX) = NULL
DECLARE @Artist VarChar(MAX) = NULL

SELECT *
FROM Music M
WHERE (M.Genre = COALESCE(@Genre, M.Genre) OR
M.Artist = COALESCE(@Artist, M.Artist))

这让我可以选择一种类型并添加一个我也想看的随机艺术家。

但是,如果我想将多个艺术家作为一个字符串传递, 即

@Genre = Rock
@Artist = 'Eric Church, Bruno Mars'

其中本质上我需要一个IN语句,但还必须处理变量为NULL的实例?

我正在使用SQL SERVER。

谢谢

2 个答案:

答案 0 :(得分:1)

使用解析/分割功能

$('#link-4').click(function(){
    $('.main').html("");
    $('.main').html('<div class="photo-gallery"><div class="photo-container"><a class="test" href="images/mosque-L.jpg"><img class="test-img" src="images/mosque-Q.jpg"></a></div><div class="photo-container"><a href="images/square-L.jpg"><img src="images/square-Q.jpg"></a></div><div class="photo-container"><a href="images/light-L.jpg"><img src="images/light-Q.jpg"></a></div><div class="photo-container"><a href="images/arch-L.jpg"><img src="images/arch-Q.jpg"></a></div><div class="photo-container"><a href="images/cows-L.jpg"><img src="images/cows-Q.jpg"></a></div><div class="photo-container"><a href="images/land-L.jpg"><img src="images/land-Q.jpg"></a></div><div class="photo-container"><a href="images/cascade-L.jpg"><img src="images/cascade-Q.jpg"></a></div><div class="photo-container"><a href="images/guard-L.jpg"><img src="images/guard-Q.jpg"></a></div><div class="photo-container"><a href="images/green-L.jpg"><img src="images/green-Q.jpg"></a></div><div class="photo-container"><a href="images/edu-L.jpg"><img src="images/edu-Q.jpg"></a></div><div class="photo-container"><a href="images/biz-L.jpg"><img src="images/biz-Q.jpg"></a></div><div class="photo-container"><a href="images/cal-L.jpg"><img src="images/cal-Q.jpg"></a></div></div><p><a href="https://www.flickr.com/photos/148227185@N05/">More on flickr</a></p>');

    $('.photo-container').magnificPopup({
        delegate: 'a', // child items selector, by clicking on it popup will open
        type: 'image',
        closeOnContentClick: true, 
        gallery:{enabled:true}
    });
});

返回

Declare @Artist varchar(max) = 'Eric Church,Bruno Mars'
Declare @Genre  varchar(max) = 'Rock'

Select Distinct A.*
  From YourTable A
  Join (
         Select * from [dbo].[udf-Str-Parse](@Artist,',') 
         Union All
         Select * from [dbo].[udf-Str-Parse](@Genre,',') 
       ) B on A.Artist = B.RetVal or A.Genre=B.RetVal

UDF(如果需要)

Genre       Artist          Song
Country     Eric Church     Record Year
Pop         Bruno Mars      Grenade
Rock        Bob Seger       Night Moves
Rock        Rolling Stones  Brown Sugar

答案 1 :(得分:1)

  1. 你可以使用CHARINDEX,但性能很低。

    SELECT *
    FROM Music M
    WHERE CHARINDEX(','+M.Genre+',',','+@Genre+',' ) >0 OR CHARINDEX(','+M.Artist+',',','+@Artist+',' ) >0
    
  2. 动态声明:

    DECLARE @Genre VarChar(MAX) = NULL
    DECLARE @Artist VarChar(MAX) = NULL
    DECLARE @sql NVARCHAR(max)
    SET @sql='
          SELECT * 
          FROM Music M
          WHERE M.Genre in ('+@Genre+') OR M.Artist in ('+@Artist+')'
    EXEC(@sql)
    
  3. 使用拆分字符串功能

  4. 处理NULL,您可以将语句更改为。

     SET @sql='
          SELECT * 
          FROM Music M
          WHERE M.Genre in ('+ISNULL(@Genre,'M.Genre')+') OR M.Artist in ('+ISNULL(@Artist,'M.Artist')+')'
    
  5. 当@Genre和@Artist为null时,脚本将在下面:

    SELECT * FROM Music M
    WHERE M.Genre in (M.Genre) OR M.Artist in (M.Artist)'
    

    1 in(1)永远是真的