2017-07-19 67 views
1

我正在查看一些逻辑,我无法弄清楚如何使这项工作。基本上我有两个SQL字符串。其中有企鹅家族的关键字和一个已经NOOT,如下图所示:这是存储在KEYWORD1 - 这是一个临时位置SQL关键字逻辑

declare @teststring varchar(512) = '{KEYWORD}' 
select top 1 k.type 
from (values 

('pingu', '66'), ('noot', '66')) 

k(word,type) where @teststring like '%' + k.word + '%' 

group by k.type 
HAVING COUNT(1) >=2 
order by COUNT(1) desc 

我的另一串有两个关键字,如:海绵和鲍勃。这是存储在KEYWORD2 - 这是一个临时位置

declare @teststring varchar(512) = '{KEYWORD}' 
select top 1 k.type 
from (values 

('sponge', '66'), ('bob', '66')) 

k(word,type) where @teststring like '%' + k.word + '%' 

group by k.type 
HAVING COUNT(1) >=2 
order by COUNT(1) desc 

现在,当我的员工使用他们有一个选项,输入一些自由文本的程序,便可以进入任何e.g海绵去小白或企鹅家族去鲍勃。

然后我有一个检查,说如果找到KEYWORD1和KEYWORD2,那么做别的事情,否则继续。这实际上是罚款下面的作品!..

declare @teststring varchar(512) = '{KEYWORD}' 
select top 1 k.type 
from (values 

('KEYWORD1', '2'), ('KEYWORD2', '2')) 

k(word,type) where @teststring like '%' + k.word + '%' 

group by k.type 
HAVING COUNT(1) >=2 
order by COUNT(1) desc 

这一切都是好的,因为它需要从每一个字和下降的正确路线。但是,如果你把海绵鲍勃放进去会发生什么,因为它试图找到海绵鲍勃和平诺诺特都会破裂。但我几乎想要3条路线,所以说话。

地说:如果平谷NOOT存在,那么继续,否则,如果海绵鲍勃存在继续或者是企鹅家族和Bob存在继续或者海绵NOOT存在继续

只是为了确认:我使用的第三方软件,我无法更新或删除任何表格

+0

我想我想知道的是,如果在文本中的任意两个字符串任意两个词存在继续,否则,如果在两个文本字符串一个字存在,那么不要继续。 – LeonNewbie

+0

你的问题不完全清楚。我接受的输入是 - pingu noot,海绵bob,pingu bob,并且这些输入中的每一个都以字符串类型的变量形式出现。另外你说可以有更多的话。你想在哪里检查这两个词是否存在?它是两个独立列(或列)组合的表中的验证吗? – VKarthik

回答

0

我为你写了一些代码,解释和例子。我也不得不加入一些假设 - 也许你已经改变的代码,如果我得到你错了:-)

/* KeyWord 1 contains 2 phrases */ 
--One has the keywords pingu and one has noot as seen below: This is stored in KEYWORD1 
DECLARE @KeyWord1_1 varchar(512) 
DECLARE @KeyWord1_2 varchar(512) 
SET @KeyWord1_1 = 'pingu' 
SET @KeyWord1_2 = 'noot' 

/* KeyWord 2 contains 2 phrases */ 
--My other string has two keywords such as: Sponge and Bob... 
DECLARE @KeyWord2_1 varchar(512) 
DECLARE @KeyWord2_2 varchar(512) 
SET @KeyWord2_1 = 'sponge' 
SET @KeyWord2_2 = 'bob' 

/* Your input-variable */ 
--Now when my staff use the program they have an option to enter some free text, they can enter anything e.g... 
DECLARE @teststring varchar(512) 
SET @teststring = 'sponge went noot or pingu goes bob.' 
-- EXAMPLES 
--SET @teststring = 'sponge bob' 
--SET @teststring = 'pingu noot' 
--SET @teststring = 'pingu bob' 
--SET @teststring = 'pingu bob' 
--SET @teststring = 'noot bob' 


DECLARE @done int 
SET @done = 0 -- we will need this for the further IFs :-) 

-- your requirement: 
-- If pingu noot exists then continue (= when the two phrases of keyword are given) 
--, otherwise if sponge bob exists continue (= when the two phrases of keyword are given) 
--or is pingu and bob exists continue 
--or if sponge and noot exists continue (also when pingu/sponge and noot/bob are given? (IF YES* IF NO**) 
-- **NO would mean: A full keyword, it can be mixed but it must fit a first and a second phrase 
-- *YES woudl mean: A whole keyword must be found, or a pharse from the first and the second keyword 
-- In my example I will stay with _NO_ 
IF (@teststring LIKE '%'[email protected]_1+'%' -- First Keyword - Full 
    AND @teststring LIKE '%'[email protected]_2+'%') 
    AND @done = 0 
BEGIN 
    PRINT 'If pingu noot exists then continue' 
    SET @done = 1; 
END 
IF (@teststring LIKE '%'[email protected]_1+'%' -- Second Keyword - Full 
    AND @teststring LIKE '%'[email protected]_2+'%') 
    AND @done = 0 
BEGIN 
    PRINT 'otherwise if sponge bob exists continue' 
    SET @done = 1; 
END 

IF (@teststring LIKE '%'[email protected]_1+'%' -- First Keyword, first Phrase and 
    AND @teststring LIKE '%'[email protected]_2+'%') -- Second Keyword, second Phrase 
    AND @done = 0 
BEGIN 
    PRINT 'pingu and bob exists continue' 
    SET @done = 1; 
END 

IF (@teststring LIKE '%'[email protected]_1+'%' -- Second Keyword, first Phrase and 
    AND @teststring LIKE '%'[email protected]_2+'%') -- First Keyword, second Phrase 
    AND @done = 0 
BEGIN 
    PRINT 'sponge and noot exists continue' 
    SET @done = 1; 
END 

IF @done = 0 
BEGIN 
    PRINT 'and i still haven''t found, what i''m looking for...' 
END 

-- If you also want to check the order (to prevent "noot pingu" from working) you can try this: 
SET @teststring = 'noot pingu' -- not working 
SET @teststring = 'pingu noot' -- it's okay 
SET @done = 0 

IF (@teststring LIKE '%'[email protected]_1+'%' -- First Keyword - Full 
    AND @teststring LIKE '%'[email protected]_2+'%') 
    AND @done = 0 
    AND CHARINDEX(@KeyWord1_1,@teststring,1) < CHARINDEX(@KeyWord1_2,@teststring,1) -- that's the ticket... 
BEGIN 
    PRINT 'If pingu noot exists - IN THE CORRECT ORDER then continue' 
    SET @done = 1; 
END 

还要考虑这仅仅是你如何可以使它的工作 - 这并不意味着是一个完整的解。 确保安全性并确保您的数据库是否区分大小写不是好方法!

问候 TGR