How to implement a Keyword Search in MySQL?

后端 未结 7 1097
温柔的废话
温柔的废话 2020-11-29 20:25

I am new to SQL programming.

I have a table job where the fields are id, position, category, location, sala

7条回答
  •  猫巷女王i
    2020-11-29 21:10

    I will explain the method i usally prefer:

    First of all you need to take into consideration that for this method you will sacrifice memory with the aim of gaining computation speed. Second you need to have a the right to edit the table structure.

    1) Add a field (i usually call it "digest") where you store all the data from the table.

    The field will look like:

    "n-n1-n2-n3-n4-n5-n6-n7-n8-n9" etc.. where n is a single word

    I achieve this using a regular expression thar replaces " " with "-". This field is the result of all the table data "digested" in one sigle string.

    2) Use the LIKE statement %keyword% on the digest field:

    SELECT * FROM table WHERE digest LIKE %keyword%
    

    you can even build a qUery with a little loop so you can search for multiple keywords at the same time looking like:

    SELECT * FROM table WHERE 
     digest LIKE %keyword1% AND 
     digest LIKE %keyword2% AND 
     digest LIKE %keyword3% ... 
    

提交回复
热议问题