SQL关键字逻辑

时间:2017-07-19 13:19:30

标签: sql tsql

我正在看一些逻辑,我无法弄清楚如何使这项工作。基本上我有两个SQL字符串。一个有关键字pingu,一个有noot,如下所示:它存储在 KEYWORD1 - 这是一个临时位置

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

我的另一个字符串有两个关键字,例如:Sponge和Bob。它存储在 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

现在,当我的员工使用该程序时,他们可以选择输入一些免费文本,他们可以输入任何东西,例如海绵去了noob或pingu去了bob。

然后我会检查一下是否找到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

这一切都很好,因为它从每个单词中取出一个单词并沿着正确的路线前进。但是......如果你放入海绵鲍勃会发生什么事情,因为它试图找到海绵鲍勃和pingu noot时打破了。但我几乎想要3条路线,所以说来。

要说:如果pingu noot存在然后继续,否则如果海绵bob存在继续或者pingu并且bob存在继续或者如果海绵和noot存在继续

JUST TO CONFIRM:我正在使用第三方软件,我无法更新或删除任何表格

1 个答案:

答案 0 :(得分: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 '%'+@KeyWord1_1+'%'  -- First Keyword - Full
  AND @teststring LIKE '%'+@KeyWord1_2+'%')
  AND @done = 0
BEGIN
    PRINT 'If pingu noot exists then continue'
    SET @done = 1;
END
IF (@teststring LIKE '%'+@KeyWord2_1+'%'  -- Second Keyword - Full
  AND @teststring LIKE '%'+@KeyWord2_2+'%')
  AND @done = 0
BEGIN
    PRINT 'otherwise if sponge bob exists continue'
    SET @done = 1;
END

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

IF (@teststring LIKE '%'+@KeyWord2_1+'%'  -- Second Keyword, first Phrase and
  AND @teststring LIKE '%'+@KeyWord1_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 '%'+@KeyWord1_1+'%'  -- First Keyword - Full
  AND @teststring LIKE '%'+@KeyWord1_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