2

我有一张如下表:

TABLE1

No  Description
1   Hello Hai Hello
2   Good Bad Good
3   Hello Good Hai
4   Hai Hello Bad
5   Hello Hello Hello

现在,如果我运行以下查询:

Select * 
from TABLE1 
where Description like '%Hai%' 
   OR  Description like '%Hello%' 
   OR Description like '%Good%'

我希望结果如下(通过添加一个额外的列“FIRST MATCHED WORD”):

No  Description     First Matched Word
1   Hello Hai Hello     Hai
2   Good Bad Good       Good
3   Hello Good Hai      Hai
4   Hai Hello Bad       Hai
5   Hello Hello Hello   Hello

听到我的意图是找出第一个匹配的单词取决于我们的查询。请帮助我。

4

5 回答 5

3

您可以通过以下方式CASE statementSELECT查询中使用:

Select No,Description, 
  CASE 
      WHEN Description like '%Hai%' THEN 'Hai' 
      WHEN Description like '%Hello%' THEN 'Hello'
      WHEN Description like '%Good%' THEN 'Good'
   END 
from TABLE1 
where Description like '%Hai%' 
   OR  Description like '%Hello%' 
   OR Description like '%Good%'

请参阅SQLFIDDLE

Case Statement返回计算结果为 的第一个布尔表达式的结果TRUE

于 2012-11-05T06:45:22.507 回答
2

尝试这个:

select *,'Hai' as 'First Matched Word' from Table1 where [Description] like '%Hai%'
union
select *,'Hello' as 'First Matched Word' from Table1 where [Description] like '%hello%' 
and [No] not in(select [No] from Table1 where [Description] like '%Hai%')
union
select *,'Good' as 'First Matched Word' from Table1 where [Description] like '%Good%' 
and [No] not in(select [No] from Table1 where [Description] like '%hello%' 
or [Description] like '%Hai%'
);

小提琴演示

于 2012-11-05T07:18:39.967 回答
2

尝试这个:

如果要添加更多单词进行匹配,只需将单词添加到具有排名的优先级表中即可。如果你有大量的单词要匹配,你可以将优先表作为永久表。

with precedence as(
select 1 rnk, 'Hai' as word union all
select 2 rnk, 'Hello' union all
select 3 rnk, 'Good' ),
cte as 
    (select * 
     from Table1
     join precedence
     on  [Description] like '%'+word+'%' ),
cte1 as(select [No],[Description],word,
               ROW_NUMBER() over (partition by [No] order by rnk) as row_num 
        from cte)
select [No],[Description],word from cte1 where row_num=1


SQL Fiddle 演示

于 2012-11-05T07:01:22.503 回答
1

尝试这个:

;WITH CTE AS
(
    SELECT
      t1.No,
      t1.Description, 
      t2.Description AS matched,
      ROW_NUMBER() OVER(PARTITION BY t1.DESCRIPTION 
                            ORDER BY t1.DESCRIPTION) row_num
    FROM @TABLE1 t1
    INNER JOIN
    (
        SELECT Description = 'Hai'
        UNION ALL 
        SELECT 'Hello'
        UNION ALL
        SELECT 'Good'
    ) t2 ON t1.Description LIKE '%' + t2.Description + '%'
)
SELECT 
  No,
  Description,
  matched AS 'first matched word'
FROM CTE
WHERE row_num = 1
ORDER BY no;

现场演示

于 2012-11-05T06:33:50.780 回答
1

如果是这种情况,您可以使用下面的 if else 语句,

if exists(select * from Table1 where description like '%Hai%')
begin
     update table1 set FirstMatchedWord = 'Hai' where No = @No
end
else
begin
     if exists(select * from Table1 where description like '%Hello%')
     begin
         update table1 set FirstMatchedWord = 'Hello' where No = @No
     end
     else
     begin
         if exists(select * from Table1 where description like '%Good%')
         begin
             update table1 set FirstMatchedWord = 'Good' where No = @No
         end
     end
end

希望这会有所帮助:D

于 2012-11-05T06:47:53.677 回答