Search string by exact word in Mysql

后端 未结 8 1170
广开言路
广开言路 2021-01-18 07:11

I have a system that searches for company. I want that when a user searches for \"Demo\", all records that have \"Demo\" will be returned, like \"The Demo\", \"Demo Inc.\",

相关标签:
8条回答
  • 2021-01-18 07:21

    I dont see why 1M is a problem I just tested on my laptop MySQL MyISAM that has also a company but it is 250K rows and it took 3.3 ms , and the field is not indexed. can you try folowing

    $search='Demo';
    $regex="/\b$search\b/i";
    $sql = "select * from table where company like '%$search%';
    //... get the results
    foreach($results as $companyName){
     if(preg_match($regex,$companyName,$match)){
        //here you got a match 
     }
    }
    
    0 讨论(0)
  • 2021-01-18 07:22
    SELECT *
    FROM table_name
    WHERE company LIKE "% Demo %"
        OR company LIKE "Demo %"
        OR company="Demo";
    
    0 讨论(0)
  • 2021-01-18 07:24

    Create an Full text index, and then you can search more easy.

    ALTER TABLE table ADD FULLTEXT INDEX fulltext_index;
    
    
    SELECT * FROM table WHERE MATCH (company) AGAINST ('+Demo' IN BOOLEAN MODE); 
    

    dev.mysql.com/doc/refman/5.6/en/fulltext-search.html

    0 讨论(0)
  • 2021-01-18 07:25

    You can use REGEXP and the [[:<:]] and [[:>:]] word boundary markers:

    SELECT
        *
    FROM
        `table`
    WHERE
        company REGEXP '[[:<:]]Demo[[:>:]]';
    

    Another Solution

    SELECT
        *
    FROM
        `table`
    WHERE
        company REGEXP '(^|[[:space:]])Demo([[:space:]]|$)';
    

    SQL Fiddle Demo

    0 讨论(0)
  • 2021-01-18 07:28

    Try this it may help ya..

     SELECT * FROM table_name WHERE company LIKE "%Demo%";
    
    0 讨论(0)
  • 2021-01-18 07:29

    A shot in the dark as per my comment. If you're always going to get an exact match criteria. would it not be best to perform a standard select query?

    SELECT * FROM table WHERE company='The Demo'
    

    Or for practical:

       $Search = $_GET['company'];
       SELECT * FROM table WHERE company='$Search'
    

    Obviously use best practices when working with user input & Queries.

    The results drawn will be either the rows found to have The Demo, the demo would be returned or nothing.


    If you do not always have an exact match. You could again, use $_GET with an appended value ie $_GET['Exact'] & have two different functions:

    function ExactMatch ($DB,$Company){
      /* 
        Query to get exact match as exampled 
      */
    
    }
    function NotExact($DB,$Company){
      /*
       Query using LIKE syntax 
      */
    }
    

    and validate:

    if (isset($_GET['Exact'])){
      if ($_GET['Exact'] === 1){
        ExactMatch($DB,$_GET['Company']);
      }else{
        NotExact($DB,$_GET['Company'])
      }
    }
    

    Also, a possible read on DBA.stackexchange:

    https://dba.stackexchange.com/questions/39693/how-to-speed-up-queries-on-a-large-220-million-rows-table-9-gig-data

    0 讨论(0)
提交回复
热议问题