【问题标题】:Match statement using * and % neither returning precise results使用 * 和 % 的匹配语句既不返回精确结果
【发布时间】:2013-07-31 20:20:10
【问题描述】:

在这个 ssql 查询中,我在 AGAINST 子句中使用了 %

            SELECT firstname,lastname,middlename,company_name, 
                    primary_emailaddress,alternate_emailaddress,personal_address_line1,
                    personal_address_line2,personal_address_city,facebook_username,
                    twitter_username,googleplus_username,linkedin_username,
                    personal_website_url,birthday_month,notes,personal_address_zipcode,
                    company_address_zipcode,home_phonenumber,company_phonenumber,
                    cell_phonenumber,birthday_day,birthday_year,hash,image_file
             FROM contacts
             WHERE (
                MATCH(
                    firstname,middlename,lastname,
                    primary_emailaddress,alternate_emailaddress,personal_address_line1,
                    personal_address_city,company_name,
                    company_address_line1,company_address_city,
                    facebook_username,twitter_username,googleplus_username,linkedin_username,
                    personal_website_url,birthday_month,notes
                )
                AGAINST ('someemail@email.com%' IN BOOLEAN MODE) 
                OR personal_address_zipcode REGEXP('(someemail@email.com*)') 
                OR company_address_zipcode REGEXP('(someemail@email.com*)') 
                OR home_phonenumber REGEXP('(someemail@email.com*)') 
                OR company_phonenumber REGEXP('(someemail@email.com*)') 
                OR cell_phonenumber REGEXP('(someemail@email.com*)') 
                OR birthday_day REGEXP('(someemail@email.com*)') 
                OR birthday_year REGEXP('(someemail@email.com*)') 
            ) 
            AND addressbook_id = 4

在这个 ssql 查询中,我在 AGAINST 子句中使用了 *

            SELECT firstname,lastname,middlename,company_name, 
                    primary_emailaddress,alternate_emailaddress,personal_address_line1,
                    personal_address_line2,personal_address_city,facebook_username,
                    twitter_username,googleplus_username,linkedin_username,
                    personal_website_url,birthday_month,notes,personal_address_zipcode,
                    company_address_zipcode,home_phonenumber,company_phonenumber,
                    cell_phonenumber,birthday_day,birthday_year,hash,image_file
             FROM contacts
             WHERE (
                MATCH(
                    firstname,middlename,lastname,
                    primary_emailaddress,alternate_emailaddress,personal_address_line1,
                    personal_address_city,company_name,
                    company_address_line1,company_address_city,
                    facebook_username,twitter_username,googleplus_username,linkedin_username,
                    personal_website_url,birthday_month,notes
                )
                AGAINST ('someemail@email.com*' IN BOOLEAN MODE) 
                OR personal_address_zipcode REGEXP('(someemail@email.com*)') 
                OR company_address_zipcode REGEXP('(someemail@email.com*)') 
                OR home_phonenumber REGEXP('(someemail@email.com*)') 
                OR company_phonenumber REGEXP('(someemail@email.com*)') 
                OR cell_phonenumber REGEXP('(someemail@email.com*)') 
                OR birthday_day REGEXP('(someemail@email.com*)') 
                OR birthday_year REGEXP('(someemail@email.com*)') 
            ) 
            AND addressbook_id = 4

两者都不会返回内容精确等于至少someemail@email.com 的位置。它通过 com 或电子邮件或其他方式返回所有内容。我需要做哪些改变?匹配列上有一个FULLTEXT 索引。

【问题讨论】:

    标签: php mysql sql full-text-search match


    【解决方案1】:

    最好将地址放在引号中,并且根本不要使用通配符:

    AGAINST ('"someemail@email.com"' IN BOOLEAN MODE)
    

    这样。

    而且我认为您不需要布尔模式。

    【讨论】:

      【解决方案2】:

      您的全文搜索不起作用,因为@.(实际上是大多数非字母数字字符)是单词分隔符。因此 1. 电子邮件被索引为三个单独的单词,并且 2. 单词分隔符从您的搜索字符串中被忽略。

      只需在您的列上创建一个常规的多列索引,然后使用标准的LIKE 进行搜索:

      WHERE firstname LIKE 'someemail@email.com%' OR ...
      

      此查询将能够使用索引,搜索将非常高效。

      另外,摆脱这些REGEXP()。它们是无用的(冗余的)并且会扼杀你的表现(不能使用索引)。

      【讨论】:

        猜你喜欢
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        • 2021-12-10
        • 2011-05-25
        • 2012-04-30
        • 2014-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多