简体   繁体   English

通配符SQL查询-缩小搜索结果

[英]Wildcard SQL Query - Narrowing Search Results

I am making a wildcard search query on the Wordpress database for "Post Title". 我在Wordpress数据库上针对“帖子标题”进行通配符搜索查询。

My results would vary: 我的结果会有所不同:

  • "Post Title - 1" “帖子标题-1”
  • "Post Title 2" “帖子标题2”
  • "Post Title #3" “帖子标题#3”
  • "After the Post Title 1" “在职位标题1之后”
  • "Before the Post Title 1" “在职务1之前”
  • "After the Post Title 2" “在职位标题2之后”
  • "Before the Post Title 2" “在职务2之前”

I would like to limit my wildcard results to: 我想将通配符结果限制为:

  • "Post Title - 1" “帖子标题-1”
  • "Post Title 2" “帖子标题2”
  • "Post Title #3" “帖子标题#3”

Any idea or algorithm I can achieve this? 有什么想法或算法可以实现吗?


Code

$query = "SELECT ID, `post_title` FROM `wp_posts` WHERE `post_title` LIKE '%".$titleRequest."%'";

$results = $wpdb->get_results($query);

Modify the query like this: 像这样修改查询:

$query = "SELECT ID, `post_title` FROM `wp_posts` WHERE `post_title` LIKE '".$titleRequest."%'";

$results = $wpdb->get_results($query);

The initial % means "match anything here" - but you do not want to do that, you want to match only things that start with the title requested. 最初的%表示“在这里匹配任何内容”-但是您不想这样做,只想匹配以请求的标题开头的内容。 The second % is ok - you do want to match things after Post Title, after all. 第二个%还可以-毕竟您确实想匹配帖子标题之后的内容。

在您的SQL查询中的WHERE子句中尝试以下操作:

WHERE FieldName LIKE 'Post Title%';

You will need to implement fuzzy logic in your search. 您将需要在搜索中实现模糊逻辑 But its a very complex algorithm, fortunately there are already some plugins that have implimented this algorithm like " Relevanssi ". 但是它是一个非常复杂的算法,所幸的是,已经有一些插件像“ Relevanssi隐含了这种算法。 Once installed it will override your current search mechanism and add its own, then you can modify it accordingly. 安装后,它将覆盖您当前的搜索机制并添加自己的搜索机制,然后您可以进行相应的修改。

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

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