如何根据SQL中某些单词的出现显示句子或行?

时间:2018-10-14 19:09:11

标签: sql-server sql-server-2014

我有一个这样的表:

  ID     Sentence
  1      A fox jumped out of the car, and ran away. 
  2      An Elephant was seen in the jungle. 
  3      A fox jumped out of the car, and ran towards jungle. 
  4      A dog was inside the car, and ran towards jungle. 

我需要搜索汽车,奔跑和丛林等单词,这些单词应在给定的句子中出现。

我的输出应该是以下几行,因为只有那些句子具有所有这些关键字:

  ID     Sentence  
  3      A fox jumped out of the car, and ran towards jungle. 
  4      A dog was inside the car, and ran towards jungle. 

我尝试在SQL中执行此操作,它为我提供了所需的输出,但我想知道是否有更好的方法可以执行此操作。

 Create table #temp1(ID int, Sentence varchar(1000)) 

 insert into #temp1 values
  (1,      'A fox jumped out of the car, and ran away.') 
 ,(2,      'An Elephant was seen in the jungle.')
 ,(3,      'A fox jumped out of the car, and ran towards jungle.')
 ,(4,      'A dog was inside the car, and ran towards jungle.')


 select * from #temp1 
 where Sentence like '%car%' and Sentence like '%jungle%' and Sentence like '%ran%'      

我得到了想要的输出。

 ID Sentence

 3  A fox jumped out of the car, and ran towards jungle.
 4  A dog was inside the car, and ran towards jungle.

0 个答案:

没有答案