How to implement a Keyword Search in MySQL?(如何在 MySQL 中实现关键字搜索?)
问题描述
我是 SQL 编程的新手.
I am new to SQL programming.
我有一个表格作业,其中的字段是 id
、position
、category
、location
、薪资范围
, description
, refno
.
I have a table job where the fields are id
, position
, category
, location
, salary range
, description
, refno
.
我想从前端实现关键字搜索.关键字可以位于上表的任何字段中.
I want to implement a keyword search from the front end. The keyword can reside in any of the fields of the above table.
这是我尝试过的查询,但它包含如此多的重复行:
This is the query I have tried but it consist of so many duplicate rows:
SELECT
a.*,
b.catname
FROM
job a,
category b
WHERE
a.catid = b.catid AND
a.jobsalrange = '15001-20000' AND
a.jobloc = 'Berkshire' AND
a.jobpos LIKE '%sales%' OR
a.jobloc LIKE '%sales%' OR
a.jobsal LIKE '%sales%' OR
a.jobref LIKE '%sales%' OR
a.jobemail LIKE '%sales%' OR
a.jobsalrange LIKE '%sales%' OR
b.catname LIKE '%sales%'
推荐答案
对于 VARCHAR 字段上的单个关键字,您可以使用 LIKE
:
For a single keyword on VARCHAR fields you can use LIKE
:
SELECT id, category, location
FROM table
WHERE
(
category LIKE '%keyword%'
OR location LIKE '%keyword%'
)
对于描述,您通常最好添加全文索引并执行 全文搜索(仅限 MyISAM):
For a description you're usually better adding a full text index and doing a Full-Text Search (MyISAM only):
SELECT id, description
FROM table
WHERE MATCH (description) AGAINST('keyword1 keyword2')
这篇关于如何在 MySQL 中实现关键字搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 MySQL 中实现关键字搜索?


- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01