[英]How can I Select all rows where column contain any words of a string
I started by asking this question I wish to Select all rows which contain any element from an array And while my current question is almost identical, it serves a whole different purpose, and so I fear the answer may be different which would obscure the previous one, with that said. 我首先问这个问题, 我希望从数组中选择所有包含任何元素的行 。虽然我当前的问题几乎相同,但它的目的完全不同,所以我担心答案可能会有所不同,这会使前一个问题难以理解, 照这样说。
I am working on a search engine type thing. 我正在研究搜索引擎类型的东西。 I need to search a title stored in a database to see if it contains any of the words in the search textbox. 我需要搜索存储在数据库中的标题,以查看其是否包含搜索文本框中的任何单词。 I plan to then iterate through each of those rows, counting how many words match and ranking the results accordingly. 然后,我计划遍历每行,计算匹配的单词数并相应地对结果进行排名。 I am sure this has been done many times by now. 我敢肯定,到现在为止已经做了很多次了。 I just can't seem to figure out how to do it without 我似乎无法弄清楚该如何做
select * from table
then sorting it in c# 然后在C#中对其进行排序
one way in sql server is sql server中的一种方法是
push the word in temp table like this 像这样在临时表中推送单词
DECLARE @SearchWords TABLE (
word varchar(30) )
INSERT INTO @SearchWords
(word)
VALUES ('Jack')
,('Pontiac')
,('Bloggs');
than join this table with the actual table 而不是将此表与实际表连接
SELECT a.* FROM table_data a
INNER JOIN @SearchWords b on a.record_desc like '%' + b.word + '%'
You could try something like this: 您可以尝试这样的事情:
SELECT * FROM table WHERE name LIKE '%word1%' OR name LIKE '%word2%';
The %
signs are the analogous to *
in typical searches. 在%
标志是类似于*
在常规搜索。
To take the string and create this query for multiple words you could do something like this: 要获取字符串并为多个单词创建此查询,您可以执行以下操作:
String inputString = "hello my friend";
String[] wordlist = inputString.split(" ");
String query = "SELECT * FROM table WHERE 0=0 ";
for(String word : wordlist) {
query += "OR name LIKE '%" + word + "%' ";
}
The spaces at the end of the strings are important, don't forget those! 字符串末尾的空格很重要,请不要忘记这些! Also, you'll probably want to sanitize the string before you split it (remove extra punctuation, maybe even remove words like "a" or "the" or "to" etc.) 另外,您可能需要在分割字符串之前先进行清理(删除多余的标点符号,甚至删除诸如“ a”,“ the”或“ to”之类的字词)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.