1

要求是获取用户提供的关键字列表,并返回在 16 个字段中的任何一个字段中包含任何关键字的任何记录。

所以如果用户输入cup dog bread
的关键词(关键词的顺序不重要,没有任何隐含关系。)

所以 WHERE 条件的数量是 78:

WHERE ObjObjectName LIKE '%cup%'
   OR ObjObjectName LIKE '%dog%'
   OR ObjObjectName LIKE '%bread%'
   OR ObjObjectOtherName LIKE '%cup%'
   OR ObjObjectOtherName LIKE '%dog%'
   OR ObjObjectOtherName LIKE '%bread%'
   OR ObjObjectID LIKE '%cup%'
   OR ObjObjectID LIKE '%dog%'
   OR ObjObjectID LIKE '%bread%'
   OR ObjOtherName LIKE '%cup%'
   OR ObjOtherName LIKE '%dog%'
   OR ObjOtherName LIKE '%bread%'
   OR ObjTitle LIKE '%cup%'
   OR ObjTitle LIKE '%dog%'
   OR ObjTitle LIKE '%bread%'
   OR ObjCategory LIKE '%cup%'
   OR ObjCategory LIKE '%dog%'
   OR ObjCategory LIKE '%bread%'
   OR AccCreditLineLocal LIKE '%cup%'
   OR AccCreditLineLocal LIKE '%dog%'
   OR AccCreditLineLocal LIKE '%bread%'
   OR PrimaryCreator LIKE '%cup%'
   OR PrimaryCreator LIKE '%dog%'
   OR PrimaryCreator LIKE '%bread%'
   OR EdiPublisherLocalSummary LIKE '%cup%'
   OR EdiPublisherLocalSummary LIKE '%dog%'
   OR EdiPublisherLocalSummary LIKE '%bread%'
   OR EdiPlaceOfPublication LIKE '%cup%'
   OR EdiPlaceOfPublication LIKE '%dog%'
   OR EdiPlaceOfPublication LIKE '%bread%'
   OR CreContinent LIKE '%cup%'
   OR CreContinent LIKE '%dog%'
   OR CreContinent LIKE '%bread%'
   OR CreSubContinent LIKE '%cup%'
   OR CreSubContinent LIKE '%dog%'
   OR CreSubContinent LIKE '%bread%'
   OR CreCountry LIKE '%cup%'
   OR CreCountry LIKE '%dog%'
   OR CreCountry LIKE '%bread%'
   OR CreRegion LIKE '%cup%'
   OR CreRegion LIKE '%dog%'
   OR CreRegion LIKE '%bread%'
   OR CreCounty LIKE '%cup%'
   OR CreCounty LIKE '%dog%'
   OR CreCounty LIKE '%bread%'
   OR CreStateProvince LIKE '%cup%'
   OR CreStateProvince LIKE '%dog%'
   OR CreStateProvince LIKE '%bread%'
   OR CreCity LIKE '%cup%'
   OR CreCity LIKE '%dog%'
   OR CreCity LIKE '%bread%'
   OR CreContinent1 LIKE '%cup%'
   OR CreContinent1 LIKE '%dog%'
   OR CreContinent1 LIKE '%bread%'
   OR CreSubContinent1 LIKE '%cup%'
   OR CreSubContinent1 LIKE '%dog%'
   OR CreSubContinent1 LIKE '%bread%'
   OR CreCountry1 LIKE '%cup%'
   OR CreCountry1 LIKE '%dog%'
   OR CreCountry1 LIKE '%bread%'
   OR CreRegion1 LIKE '%cup%'
   OR CreRegion1 LIKE '%dog%'
   OR CreRegion1 LIKE '%bread%'
   OR CreCounty1 LIKE '%cup%'
   OR CreCounty1 LIKE '%dog%'
   OR CreCounty1 LIKE '%bread%'
   OR CreStateProvince1 LIKE '%cup%'
   OR CreStateProvince1 LIKE '%dog%'
   OR CreStateProvince1 LIKE '%bread%'
   OR CreCity1 LIKE '%cup%'
   OR CreCity1 LIKE '%dog%'
   OR CreCity1 LIKE '%bread%'
   OR CreDateCreated LIKE '%cup%'
   OR CreDateCreated LIKE '%dog%'
   OR CreDateCreated LIKE '%bread%'
   OR CreMarkSignature LIKE '%cup%'
   OR CreMarkSignature LIKE '%dog%'
   OR CreMarkSignature LIKE '%bread%'

这真的是最好的方法吗?我的意思是,我打算使用 PDO 准备语句,我发现这个问答(使用 PDO 准备语句的搜索字段中的多个关键字进行 LIKE 查询)这将非常有帮助,但我对 MySQL 相对缺乏经验,所以我不确定是否有是处理所有条件的更好方法。谢谢!

4

2 回答 2

1

您可以使用正则表达式和连接:

WHERE CONCAT(
  ObjObjectName, 
  ObjObjectOtherName, 
  ObjObjectID, 
  ObjOtherName,
  ObjTitle,
  ObjCategory,
  AccCreditLineLocal,
  PrimaryCreator,
  EdiPublisherLocalSummary,
  EdiPlaceOfPublication,
  CreContinent,
  CreSubContinent)
  REGEXP 'cup|dog|bread'
于 2013-03-08T19:02:51.780 回答
0

您需要使用全文搜索。请记住,MySql 的全文索引仅限于 4 个以上的字母单词。所以“狗”是行不通的。你可以编译 MySQL 并改变它。Sphinx通常更适合较小的单词和更快的查找。

我还建议查看MariaDB,它是 MySQL 的一个替代品,由原始 MySQL 开发人员创建。MariaDB 有一个Sphinx 存储引擎,这使得集成更容易。

于 2013-03-08T18:54:35.777 回答