简体   繁体   English

如何选择列包含字符串中任何单词的所有行

[英]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.

相关问题 如何在以特定字符串开头的字符串字段中选择所有行 - How to select all rows WHERE a string filed start with a specific string 如何检查列表中的任何单词是否包含部分字符串? - How to check if any words in a list contain a partial string? 如何连接一行中的所有列值,然后将DataTable中的所有行连接成一个字符串? - How can I concatenate all the column values in a row, and then concatenate all rows in a DataTable into a single string? 如何从指定单词之间选择字符串 - How can I select a string from between specified words 如何将字符串中所有单词的首字母大写? - How can I uppercase the first letter of all words in my string? 我如何在Linq中排序该列为字符串的列? - How can I order a column in Linq where that column is a string? 选择列等于数组中字符串值之一的行 - Select rows where column equals one of the string values in array 如何在运行时为OrderBy / Where选择属性(不带字符串参数)? - How can I select the property for OrderBy/Where at runtime (not with a string parameter)? 如何 select 包含数组中字符串的所有名称? (EF 和 Linq) - How to select all Names that contain a string from an array? (EF and Linq) 仅选择任何孩子的名字不包含特定字符串的父母 - Select only the parents where any of the children's names do not contain a specific string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM