简体   繁体   English

在数据库中搜索整个字符串/短语

[英]Searching in database for entire string / phrase

I have a form where there are two radio buttons . 我有一个表格,其中有两个单选按钮

One should search for entire string and second one for at least one word from string. 一个应搜索整个字符串第二个 应搜索字符串 中至少一个单词

For example: 例如:

I will search for " Awesome ideas for startup ". 我将搜索“ 很棒的启动创意 ”。

If I check "Search for entire string / phrase", where it must contain exactly what I typed. 如果我选中“搜索整个字符串/短语”,则其中必须包含我键入的内容。

  • I want return something like "There where many awesome ideas for startup ". 我想要返回类似“那里有很多很棒的启动想法 ”的信息。
  • Or " Awesome ideas for startup that will blow your mind". 或“ 很棒的启动创意会让您大吃一惊”。

If I check "Search for all words contained". 如果我选中“搜索包含的所有单词”。

  • I want return something like "This startup is amazing". 我想返回类似“这家公司很棒”的信息。

I don't have problem with submitting the form or anything like that, but problem with SQL query that will return results which I mentioned above. 我没有提交表单或类似的东西的问题,但是SQL查询会返回我上面提到的结果的问题。

if ($_POST['selectedRadio'] == 'entireString') { 
   $query = '/* QUERY HERE */';
} elseif($_POST['selectedRadio'] == 'containedWords') {
   $query = '/* QUERY HERE */';
}

Is there any efficient and fast way to get those wished results? 是否有任何有效,快速的方法来获得期望的结果?

Thanks. 谢谢。

SELECT * FROM table_name WHERE column_name LIKE %your_string%

This code will search in table exactly what you're looking for, string or phrase. 此代码将在表中精确搜索您要查找的内容(字符串或短语)。 Google for sql like command and build query what you need. Google for sql之类的命令和构建查询,您需要什么。

Already solved this problem. 已经解决了这个问题。

"This startup is amazing". “这个创业公司真是太神奇了” - Managed to return this with Fulltext search. -设法通过全文本搜索返回此内容。

"There where many awesome ideas for startup". “那里有许多很棒的启动创意”。 - Managed to return this with LIKE condition. -设法以类似条件返回此商品。

Thanks for help everybody. 感谢大家的帮助。

您可以使用(.isSpace)定位分隔每个单词的白色间距,这不是确切的答案,但是您可以从这种方式开始研究。

String str = "your string";
String[] separator = str.split(" ");//this will split string with space " "
String firstword = separator  [0]; // "your"
String secondword = separator [1]; // "string"

whatever you'' put in string.split will be used as divider,splitter 无论您将什么放在string.split中,都将用作分隔线,分隔线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM