热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

多个关键字搜索脚本需要一些专家输入-multiplekeywordsearchscriptneedssomeexpertinput

Ihavebeenwrittingakeywordsearchscriptbasedonthistutorial:http:www.hackosis.com200711

I have been writting a keyword search script based on this tutorial: http://www.hackosis.com/2007/11/06/howto-simple-search-engine-with-php-and-mysql/

我一直在编写基于本教程的关键字搜索脚本:http://www.hackosis.com/2007/11/06/howto-simple-search-engine-with-php-and-mysql/

Like some of the commenters mentioned, the script only ends up returning results based on the last word in the search terms. So I have also tried to implement one of the suggestions from another user, but now I seem to only be able to get results based on the first search term.

与提到的一些评论者一样,脚本最终只会根据搜索字词中的最后一个字返回结果。所以我也试图实现其他用户的一个建议,但现在我似乎只能根据第一个搜索词得到结果。

The code for my script can be found here: http://php.pastebin.com/m7759afd3

我的脚本代码可以在这里找到:http://php.pastebin.com/m7759afd3

My print_r for the results looks like this:

我的print_r结果如下:

SELECT * FROM blog WHERE blog_title LIKE '%barry%' OR blog_content LIKE '%barry%' AND blog_title LIKE '%child%' OR blog_content LIKE '%child%' AND blog_title LIKE '%help%' OR blog_content LIKE '%help%' ORDER BY blog_title

SELECT * FROM links WHERE link_title LIKE '%barry%' OR link_desc LIKE '%barry%' AND link_title LIKE '%child%' OR link_desc LIKE '%child%' AND link_title LIKE '%help%' OR link_desc LIKE '%help%' ORDER BY link_title

SELECT * FROM pages WHERE page_title LIKE '%barry%' OR page_content LIKE '%barry%' AND page_title LIKE '%child%' OR page_content LIKE '%child%' AND page_title LIKE '%help%' OR page_content LIKE '%help%' ORDER BY page_title

Thank you for any help you might be able to offer.

感谢您提供的任何帮助。

2 个解决方案

#1


3  

If you want to return results that contain any of the keywords, then replace all the AND with OR.

如果要返回包含任何关键字的结果,请将所有AND替换为OR。

If you do this, then your db will check if any of the keywords exist in any of the columns of the table.

如果这样做,那么您的数据库将检查表中任何列中是否存在任何关键字。

Hence your final query would be read by the DB server like this:

因此,您的最终查询将由DB服务器读取,如下所示:

SELECT * FROM blog WHERE blog_title LIKE '%barry%' OR blog_content LIKE '%barry%' OR blog_title LIKE '%child%' OR blog_content LIKE '%child%' OR blog_title LIKE '%help%' OR blog_content LIKE '%help%' ORDER BY blog_title

and should return all that records that have the keywords (barry, child, help) in any column (blog_title, blog_content).

并且应该在任何列(blog_title,blog_content)中返回所有具有关键字(barry,child,help)的记录。

Hope this helps.

希望这可以帮助。

#2


1  

The AND operator has a higher precedence than OR, so if you want to return results that contain all of the keywords you need to add parentheses as follows:

AND运算符的优先级高于OR,因此如果要返回包含所有关键字的结果,则需要添加括号,如下所示:

  SELECT * FROM blog
  WHERE (blog_title LIKE '%barry%' OR blog_content LIKE '%barry%')
    AND (blog_title LIKE '%child%' OR blog_content LIKE '%child%')
    AND (blog_title LIKE '%help%' OR blog_content LIKE '%help%')
  ORDER BY blog_title

推荐阅读
  • 根据最新发布的《互联网人才趋势报告》,尽管大量IT从业者已转向Python开发,但随着人工智能和大数据领域的迅猛发展,仍存在巨大的人才缺口。本文将详细介绍如何使用Python编写一个简单的爬虫程序,并提供完整的代码示例。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • PHP 编程疑难解析与知识点汇总
    本文详细解答了 PHP 编程中的常见问题,并提供了丰富的代码示例和解决方案,帮助开发者更好地理解和应用 PHP 知识。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 在维护公司项目时,发现按下手机的某个物理按键后会激活相应的服务,并在屏幕上模拟点击特定坐标点。本文详细介绍了如何使用ADB Shell Input命令来模拟各种输入事件,包括滑动、按键和点击等。 ... [详细]
  • 使用Pandas高效读取SQL脚本中的数据
    本文详细介绍了如何利用Pandas直接读取和解析SQL脚本,提供了一种高效的数据处理方法。该方法适用于各种数据库导出的SQL脚本,并且能够显著提升数据导入的速度和效率。 ... [详细]
  • 本文由杨勇和思远于2012年12月27日撰写,主要探讨了如何使用PHP进行网页内容抓取,特别是针对字符较多的网站。文章详细介绍了正则表达式失效的原因,并提供了优化方法,同时展示了如何抓取淘宝服饰栏、天气信息以及IP地址对应的地理位置。 ... [详细]
  • 深入理解Lucene搜索机制
    本文旨在帮助读者全面掌握Lucene搜索的编写步骤、核心API及其应用。通过详细解析Lucene的基本查询和查询解析器的使用方法,结合架构图和代码示例,带领读者深入了解Lucene搜索的工作流程。 ... [详细]
  • 利用决策树预测NBA比赛胜负的Python数据挖掘实践
    本文通过使用2013-14赛季NBA赛程与结果数据集以及2013年NBA排名数据,结合《Python数据挖掘入门与实践》一书中的方法,展示如何应用决策树算法进行比赛胜负预测。我们将详细讲解数据预处理、特征工程及模型评估等关键步骤。 ... [详细]
  • ?keyword=%CE%F7%B9%CF可用    ?keyword=西瓜 不可用,该如何解决
    后端开发|php教程nbsp,keyword,smarty,CF,echo后端开发-php教程?keyword%CE%F7%B9%CF可用?keyword西瓜不可用今天在编程序的时 ... [详细]
  • Whatarethedifferencesbetweenthesefourinline(key)words?这四个内联(关键字)的区别是什么?inline,__inline, ... [详细]
author-avatar
Mr_ZERO0000000
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有