是否有更好的方法来构建具有广泛WHERE条件的MySQL查询?

时间:2013-03-08 18:51:03

标签: mysql where

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

因此,如果用户输入杯狗面包的关键字
(关键字的顺序并不重要,并且它们之间没有隐含的关系。)

所以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编写的语句,我发现这个Q& A(LIKE query using multiple keywords from search field using PDO prepared statement)会非常有帮助,但我对MySQL相对缺乏经验,所以我不确定是否有更好的方法来处理所有那些有条件的人。谢谢!

2 个答案:

答案 0 :(得分:1)

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

WHERE CONCAT(
  ObjObjectName, 
  ObjObjectOtherName, 
  ObjObjectID, 
  ObjOtherName,
  ObjTitle,
  ObjCategory,
  AccCreditLineLocal,
  PrimaryCreator,
  EdiPublisherLocalSummary,
  EdiPlaceOfPublication,
  CreContinent,
  CreSubContinent)
  REGEXP 'cup|dog|bread'

答案 1 :(得分:0)

您需要使用Full-Text search。请记住,MySql的全文索引限制为4个以上的字母单词。所以“狗”不起作用。你可以编译MySQL并改变它。 Sphinx通常更适合较小的单词和更快的查找。

我还建议查看由原始MySQL开发人员创建的MariaDB,这是MySQL的替代品。 MariaDB有Sphinx storage engine,这使得集成更容易。