简体   繁体   中英

Mysql query search a string in all columns of a table

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           | text      | Date       | Author  | Status
1  | Augusto Weiand | Test text | 2010-01-01 | Deividi | 1

Query

SELECT * 
FROM table 
WHERE columns LIKE '%augusto%2010%text%"

I did not put enough detail, excuse me, I like to make a dynamic SQL, where I do not need to specify the columns with 'AND' or 'OR', as it is possible to do in Postgres:

Select * 
From table 
Where table::text ~~ '%augusto%2010%text%'

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%"

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 .

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 (;-).

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM