Mysql query search a string in all columns of a table

后端 未结 4 1351
一向
一向 2021-01-27 11:43

I wonder how I can mount a SQL as portable as possible to query for all columns of a table for a specific phrase, like:

Table

ID | Name          


        
相关标签:
4条回答
  • 2021-01-27 12:14

    you can use this store procedure to search a text in all column in table

    CREATE PROCEDURE dbo.sp_FindStringInTable @stringToFind VARCHAR(100), 
    @schema 
    sysname, @table sysname 
    AS
    
    BEGIN TRY
    DECLARE @sqlCommand varchar(max) = 'SELECT * FROM [' + @schema + '].[' + 
    @table + '] WHERE ' 
    
    SELECT @sqlCommand = @sqlCommand + '[' + COLUMN_NAME + '] LIKE ''' + 
    @stringToFind + ''' OR '
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @schema
    AND TABLE_NAME = @table 
    AND DATA_TYPE IN ('char','nchar','ntext','nvarchar','text','varchar')
    
    SET @sqlCommand = left(@sqlCommand,len(@sqlCommand)-3)
    EXEC (@sqlCommand)
    PRINT @sqlCommand
    END TRY
    
    BEGIN CATCH 
    PRINT 'There was an error. Check to make sure object exists.'
    PRINT error_message()
    END CATCH 
    

    Execute it like this

    EXEC sp_FindStringInTable '%searchword%','schema_name', 'tablename'
    
    0 讨论(0)
  • 2021-01-27 12:20

    try this

       Select * FROM table WHERE text LIKE "%text%"
                              OR date LIKE "%2010%"
                              OR Name LIKE "%augusto%"
    

    if you want them all together then use AND

       Select * FROM table WHERE text LIKE "%text%"
                              AND date LIKE "%2010%"
                              AND Name LIKE "%augusto%"
    
    0 讨论(0)
  • 2021-01-27 12:27

    It's doable, although I strongly suggest you look into full-text search for efficiency;

    To avoid looking for all patterns in all fields one by one, you can just concat and search in that;

    SELECT *
    FROM (SELECT id,CONCAT(name,'|',text,'|',date,'|',author,'|',status) txt
          FROM Table1) a
    WHERE txt LIKE '%augusto%'
      AND txt LIKE '%2010%'
      AND txt LIKE '%text%';
    

    Note that no indexing will help you here, since you're searching in a calculated column. On the other hand, since you're searching with a leading wildcard %searchterm, you won't get much help from indexes even if searching field by field :)

    An SQLfiddle to test with.

    0 讨论(0)
  • 2021-01-27 12:37

    Here is how you would concatenate the values in dynamic SQL:

    set @Pattern = '%augusto%';
    
    select @q := concat('select * from Table1 ',
                       'where concat(', group_concat(column_name), ', "") like "', @Pattern, '"'
                       )
    from information_schema.columns c
    where table_name = 'Table1';
    
    prepare st from @q;
    execute st;
    
    deallocate prepare st;
    

    Of course, dynamic SQL is not particularly portable. The idea would work in most databases. The code would look different.

    Tested and working here.

    And finally, you can do this with variable substitution (which is the better approach):

    select @q := concat('select * from Table1 ',
                       'where concat(', group_concat(column_name), ', "") like ?'
                       )
    from information_schema.columns c
    where table_name = 'Table1';
    
    set @p = '%augusto%';
    
    prepare st from @q;
    execute st using @p;
    
    deallocate prepare st;
    

    Also tested (;-).

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