如何使用IN子句查询大量单词列表

sky*_*e01 3 sql sql-server

我在SQL Server Management Studio 2008中工作.

我需要编写一个查询,在一个表中的几个字段中搜索大量的单词列表(大约50个单词).我目前的想法是简单地写一个IN条款来放入我的所有单词.然而,IN由于我必须搜索的单词数量,创建此条款可能会变得乏味.

有没有更好的方法呢?

我当前的查询类似于以下内容:

SELECT
x,
y
FROM Table1
WHERE 
x IN ('word1', 'word2', ... , 'wordx')
OR y IN ('word1', 'word2', ... , 'wordx')
Run Code Online (Sandbox Code Playgroud)

Dal*_*len 5

您可以创建一个表(比方说word_table)并将所有单词存储在其中.然后你就可以做到

SELECT x, y
FROM Table1
WHERE 
   x IN (SELECT word FROM word_table) OR
   y IN (SELECT word FROM word_table)
Run Code Online (Sandbox Code Playgroud)

  • 为什么选择downvote?这是一个非常好的答案 (3认同)

jue*_*n d 5

select t.*
from your_table t
left join word_table wx on wx.word = t.x
left join word_table wy on wy.word = t.y
where wx.word is not null 
   or wy.word is not null 
Run Code Online (Sandbox Code Playgroud)