Search all columns of a table using a single where condition with single keyword in mysql(在mysql中使用单个关键字的单个where条件搜索表的所有列)
问题描述
我有一个包含 64 个不同字段的表格.我将在其中使用单个关键字进行搜索,结果应与任何字段中的关键字匹配.给点建议.
I have a table which consists of 64 different fields. i am going to search with a single keyword in it, Results should match the keyword from any field. Give some suggestions.
推荐答案
SELECT * FROM `some_table`
WHERE
CONCAT_WS('|',`column1`,`column2`,`column3`,`column4`,`column64`) # single condition, many columns
LIKE '%VT%'
瞧.
'|'顺便说一下,分隔符是为了防止您找到巧合的匹配,例如,第 1 列以V"结尾,第 2 列以T"开头,这会使您在搜索VT"时出现误报.
The '|' separator, by the way, is to prevent you finding coincidental matches where, e.g., column1 ends in 'V' and column2 starts with 'T', which would give you a false positive in a search for "VT".
我不确定上述方法是否比 OR 方法更快(我猜它们的速度相同),但如果您正在编写查询,它肯定涉及较少的输入手动.
I'm not sure if the above method is faster than the OR method (I would guess they're the same speed) , but it definitely involves less typing if you're writing the query by hand.
这篇关于在mysql中使用单个关键字的单个where条件搜索表的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在mysql中使用单个关键字的单个where条件搜索表的所有列
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
