我在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)
您可以创建一个表(比方说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)
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)
| 归档时间: |
|
| 查看次数: |
3175 次 |
| 最近记录: |