Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a Keyword Search in MySQL?

Tags:

sql

search

mysql

I am new to SQL programming.

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%' 
like image 675
santanu Avatar asked Apr 17 '09 08:04

santanu


People also ask

How do I create a search in MySQL?

In the search grid, choose tables and views of interest or leave them all checked. To narrow down the MySQL search data scope, select the table, views, numeric, text type, and date columns checkboxes. To start the search, click the Find button or hit the Enter key from the keyboard.

How do I search for a word in MySQL?

There is a Schemas tab on the side menu bar, click on the Schemas tab, then double click on a database to select the database you want to search. Then go to menu Database - Search Data, and enter the text you are searching for, click on Start Search.

What are keywords in MySQL?

Keywords are words that have significance in SQL. Certain keywords, such as SELECT , DELETE , or BIGINT , are reserved and require special treatment for use as identifiers such as table and column names. This may also be true for the names of built-in functions.


1 Answers

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

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') 
like image 170
Greg Avatar answered Oct 09 '22 14:10

Greg