TSQL喜欢和不喜欢用法

时间:2017-07-13 18:42:22

标签: sql-server sql-like sql-server-2016

我的链接数据如下:

Data
www.example.com/
www.example.com/someotherpath/
www.example.com/someotherpath/included
www.example.com/someexclusivepath/
www.example.com/someexclusivepath/AnyPathHereShouldBeExcluded/
www.example.com/someexclusivepath/AnyPathHereShouldBeExcluded

我正在尝试写一个Like条件,它应该只给我4个结果:

www.example.com/
www.example.com/someotherpath/
www.example.com/someotherpath/included
www.example.com/someexclusivepath/

如果我喜欢而不喜欢:

%www.example.com/%' AND Not like '%www.example.com/someexclusivepath/%'

这也将从上面排除一些链接,结果将是:

www.example.com/
www.example.com/someotherpath/
www.example.com/someotherpath/included

如何获得预期结果?

更新:

示例:

DECLARE @tbl TABLE (link varchar(200))
INSERT INTO @tbl VALUES
('www.example.com/'),
('www.example.com/someotherpath/'),
('www.example.com/someotherpath/included'),
('www.example.com/someexclusivepath/'),('www.example.com/someexclusivepath/AnyPathHereShouldBeExcluded/'),
('www.example.com/someexclusivepath/AnyPathHereShouldBeExcluded')

SELECT * FROM @tbl

SELECT * FROM @tbl
WHERE link like '%www.example.com/%' AND Link Not like '%www.example.com/someexclusivepath/%'

2 个答案:

答案 0 :(得分:1)

试试这个:

DECLARE @tbl TABLE (link varchar(200))
INSERT INTO @tbl VALUES
('www.example.com/'),
('www.example.com/someotherpath/'),
('www.example.com/someexclusivepath/'), 
('www.example.com/someotherpath/included'),    
('www.example.com/someexclusivepath/AnyPathHereShouldBeExcluded/'),
('www.example.com/someexclusivepath/AnyPathHereShouldBeExcluded')

SELECT * FROM @tbl
     WHERE (link like '%/%' or link like '%/%/') 
     and (link not like '%/%/[a-z]%' or link like '%/%/included')

在行动here中查看。

答案 1 :(得分:0)

如果它们始终相同,并以“/”符号结尾,则可以执行以下操作:

DECLARE @tbl TABLE (link varchar(200))
INSERT INTO @tbl VALUES('www.example.com/'),('www.example.com/someotherpath/'),('www.example.com/someexclusivepath/'),('www.example.com/someexclusivepath/AnyPathHereShouldBeExcluded/')
SELECT * FROM @tbl

SELECT * FROM @tbl
WHERE Link Not like 'www.example.com/someexclusivepath/%/'