在整个 Mysql table(所有字段)中搜索特定字符串
Search a particular String in entire Mysql table(All fields)
我目前正在使用以下单字段搜索查询
SELECT *
FROM user_college_tbl
WHERE college_name LIKE '%Impulse%';
如何在整个table(所有字段)中搜索特定字符串?
您可以考虑使用全文搜索:
SELECT *
FROM user_college_tbl
WHERE MATCH(col1, col2, ...) AGAINST ('Impulse' IN NATURAL LANGUAGE MODE)
假设您已在要包含在搜索中的所有文本列上设置了全文索引:
CREATE TABLE user_college_tbl (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
college_name VARCHAR(200),
col1 TEXT,
col2 TEXT,
-- ... more text columns
FULLTEXT(college_name, col1, col2, ...)
) ENGINE=InnoDB;
如果您使用的 MySQL 版本早于 5.6 或者您不想进行全文搜索,那么您可能只能对 LIKE
中每个文本列的表达式进行 ORing table,例如
SELECT *
FROM user_college_tbl
WHERE college_name LIKE '%Impulse%' OR
col1 LIKE '%Impulse%' OR
col2 LIKE '%Impulse%'
您还可以执行动态 sql 查询。
查询
set @query = null;
select
group_concat(
concat(
column_name, '', ' like \'%Impulse%\' or '
) separator ''
) into @query
from information_schema.columns
where table_name = 'user_college_tbl';
set @query = concat('select * from user_college_tbl where ',
left(@query, length(@query) - 4));
prepare stmt from @query;
execute stmt;
deallocate prepare stmt;
我目前正在使用以下单字段搜索查询
SELECT *
FROM user_college_tbl
WHERE college_name LIKE '%Impulse%';
如何在整个table(所有字段)中搜索特定字符串?
您可以考虑使用全文搜索:
SELECT *
FROM user_college_tbl
WHERE MATCH(col1, col2, ...) AGAINST ('Impulse' IN NATURAL LANGUAGE MODE)
假设您已在要包含在搜索中的所有文本列上设置了全文索引:
CREATE TABLE user_college_tbl (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
college_name VARCHAR(200),
col1 TEXT,
col2 TEXT,
-- ... more text columns
FULLTEXT(college_name, col1, col2, ...)
) ENGINE=InnoDB;
如果您使用的 MySQL 版本早于 5.6 或者您不想进行全文搜索,那么您可能只能对 LIKE
中每个文本列的表达式进行 ORing table,例如
SELECT *
FROM user_college_tbl
WHERE college_name LIKE '%Impulse%' OR
col1 LIKE '%Impulse%' OR
col2 LIKE '%Impulse%'
您还可以执行动态 sql 查询。
查询
set @query = null;
select
group_concat(
concat(
column_name, '', ' like \'%Impulse%\' or '
) separator ''
) into @query
from information_schema.columns
where table_name = 'user_college_tbl';
set @query = concat('select * from user_college_tbl where ',
left(@query, length(@query) - 4));
prepare stmt from @query;
execute stmt;
deallocate prepare stmt;