Mysql查询在表的所有列中搜索字符串

问题描述 投票:0回答:5

我想知道如何安装尽可能可移植的 SQL 来查询表的所有列中的特定短语,例如:

桌子

ID | Name           | text      | Date       | Author  | Status
1  | Augusto Weiand | Test text | 2010-01-01 | Deividi | 1

查询

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

我没有提供足够的细节,对不起,我喜欢创建动态 SQL,我不需要用“AND”或“OR”指定列,就像在 Postgres 中可以做到的:

Select * 
From table 
Where table::text ~~ '%augusto%2010%text%'
mysql sql
5个回答
3
投票

以下是如何连接动态 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;

当然,动态SQL并不是特别可移植。 这个想法适用于大多数数据库。 代码看起来会有所不同。

已测试并可工作此处

最后,您可以通过变量替换来做到这一点(这是更好的方法):

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;

还测试了(;-)。


3
投票

这是可行的,尽管我强烈建议您研究全文搜索以提高效率;

为了避免在所有字段中一一查找所有模式,您可以直接在其中进行连接和搜索;

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%';

请注意,这里没有索引对您有帮助,因为您正在计算列中搜索。另一方面,由于您使用前导通配符

%searchterm
进行搜索,因此即使逐个字段搜索,您也不会从索引中获得太多帮助:)

用于测试的 SQLfiddle。


1
投票
试试这个

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

如果您希望它们全部放在一起,请使用 AND

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

1
投票
您可以使用此存储过程在表中的所有列中搜索文本

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

像这样执行

EXEC sp_FindStringInTable '%searchword%','schema_name', 'tablename'
    

0
投票
我使用以下查询:

select group_concat( concat( 'select "',table_name,'" as table_name, "',column_name,'" as column_name, ', column_name,' as value from ',table_name,' where ',column_name,' like \'%test_value%\'' ) separator' union ' ) from information_schema.columns where information_schema.columns.table_schema = 'schema_name' and information_schema.columns.column_type not like '%int%' and information_schema.columns.column_type not like '%double%' and information_schema.columns.column_type not like '%decimal%' and information_schema.columns.column_type not like '%float%'\G
(当然,将 

schema_name

 替换为您的架构/数据库名称,并将 
test_value
 替换为您要搜索的内容。您也可以像子句一样省略 column_type ,但这会使创建的查询更大)

注意:结果将是巨大的。 使用 \G 代替 ;以避免滚动多行破折号来复制语句。

然后复制并执行该语句。

例如我将从我的数据库模式之一复制并执行以下内容。

select "node_revision__field_subject_area" as table_name,"langcode" as column_name, langcode as value from node_revision__field_subject_area where langcode like '%our license agreements prohibit posting %' union select "cache_library" as table_name,"cid" as column_name, cid as value from cache_library where cid like '%our license agreements prohibit posting %' union select "cache_library" as table_name,"data" as column_name, data as value from cache_library where data like '%our license agreements prohibit posting %' union select "cache_library" as table_name,"tags" as column_name, tags as value from cache_library where tags like '%our license agreements prohibit posting %' union select "cache_library" as table_name,"checksum" as column_name, checksum as value from cache_library where checksum like '%our license agreements prohibit posting %' union select "semaphore" as table_name,"name" as column_name, name as value from semaphore where name like '%our license agreements prohibit posting %' union select "semaphore" as table_name,"value" as column_name, value as value from semaphore where value like '%our license agreements prohibit posting %' union select "paragraph__field_position_title" as table_name,"bundle" as column_name, bundle as value from paragraph__field_position_title where bundle like '%our license agreements prohibit posting %' union select "paragraph__field_position_title" as table_name,"langcode" as column_name, langcode as value from paragraph__field_position_title where langcode like '%our license agreements prohibit posting %' union select "paragraph__field_position_title" as table_name,"field_position_title_value" as column_name, field_position_title_value as value from paragraph__field_position_title where field_position_title_value like '%our license agreements prohibit posting %';
当然,这个示例仅占我们实际 drupal 数据库生成的查询的 5%,该数据库拥有超过 2750 个不同的字段。

© www.soinside.com 2019 - 2024. All rights reserved.