Search a whole table in mySQL for a string

后端 未结 8 1592
孤城傲影
孤城傲影 2020-12-14 01:13

I\'m trying to search a whole table in mySQL for a string.

I want to search all fields and all entrees of a table, returning each full entry that contains the speci

相关标签:
8条回答
  • 2020-12-14 01:44

    Try this code,

    SELECT 
           * 
    FROM 
           `customers` 
    WHERE 
           (
              CONVERT 
                   (`customer_code` USING utf8mb4) LIKE '%Mary%' 
              OR 
              CONVERT(`customer_name` USING utf8mb4) LIKE '%Mary%' 
              OR 
              CONVERT(`email_id` USING utf8mb4) LIKE '%Mary%' 
              OR 
              CONVERT(`address1` USING utf8mb4) LIKE '%Mary%' 
              OR
              CONVERT(`report_sorting` USING utf8mb4) LIKE '%Mary%'
           )
    

    This is help to solve your problem mysql version 5.7.21

    0 讨论(0)
  • 2020-12-14 01:45

    If you are just looking for some text and don't need a result set for programming purposes, you could install HeidiSQL for free (I'm using v9.2.0.4947).

    Right click any database or table and select "Find text on server".

    All the matches are shown in a separate tab for each table - very nice.

    Frighteningly useful and saved me hours. Forget messing about with lengthy queries!!

    0 讨论(0)
  • 2020-12-14 01:47

    Identify all the fields that could be related to your search and then use a query like:

    SELECT * FROM clients
    WHERE field1 LIKE '%Mary%'
       OR field2 LIKE '%Mary%'
       OR field3 LIKE '%Mary%'
       OR field4 LIKE '%Mary%'
       ....
       (do that for each field you want to check)
    

    Using LIKE '%Mary%' instead of = 'Mary' will look for the fields that contains someCaracters + 'Mary' + someCaracters.

    0 讨论(0)
  • 2020-12-14 01:50

    Try something like this:

    SELECT * FROM clients WHERE CONCAT(field1, '', field2, '', fieldn) LIKE "%Mary%"
    

    You may want to see SQL docs for additional information on string operators and regular expressions.

    Edit: There may be some issues with NULL fields, so just in case you may want to use IFNULL(field_i, '') instead of just field_i

    Case sensitivity: You can use case insensitive collation or something like this:

    ... WHERE LOWER(CONCAT(...)) LIKE LOWER("%Mary%")
    

    Just search all field: I believe there is no way to make an SQL-query that will search through all field without explicitly declaring field to search in. The reason is there is a theory of relational databases and strict rules for manipulating relational data (something like relational algebra or codd algebra; these are what SQL is from), and theory doesn't allow things such as "just search all fields". Of course actual behaviour depends on vendor's concrete realisation. But in common case it is not possible. To make sure, check SELECT operator syntax (WHERE section, to be precise).

    0 讨论(0)
  • 2020-12-14 01:51

    In addition to pattern matching with 'like' keyword. You can also perform search by using fulltext feature as below;

    SELECT * FROM clients WHERE MATCH (shipping_name, billing_name, email) AGAINST ('mary')
    
    0 讨论(0)
  • 2020-12-14 01:54

    A PHP Based Solution for search entire table ! Search string is $string . This is generic and will work with all the tables with any number of fields

    $sql="SELECT * from client_wireless";
    $sql_query=mysql_query($sql);
    $logicStr="WHERE ";
    $count=mysql_num_fields($sql_query);
    for($i=0 ; $i < mysql_num_fields($sql_query) ; $i++){
     if($i == ($count-1) )
    $logicStr=$logicStr."".mysql_field_name($sql_query,$i)." LIKE '%".$string."%' ";
    else
    $logicStr=$logicStr."".mysql_field_name($sql_query,$i)." LIKE '%".$string."%' OR ";
    }
    // start the search in all the fields and when a match is found, go on printing it .
    $sql="SELECT * from client_wireless ".$logicStr;
    //echo $sql;
    $query=mysql_query($sql);
    
    0 讨论(0)
提交回复
热议问题