Best way to do a weighted search over multiple fields in mysql?

后端 未结 6 1166
轮回少年
轮回少年 2020-12-04 14:42

Here\'s what i want to do:

  • match a search subject against multiple fields of my table
  • order the results by importance of the field and relevance of th
6条回答
  •  情歌与酒
    2020-12-04 15:34

    you can add multiple mysql MATCH() values together, first multiplying each one by their weight.

    simplified of course...

    '(MATCH(column1) AGAINST(\''.$_GET['search_string'].'\') * '.$column1_weight.')
     + (MATCH(column2) AGAINST(\''.$_GET['search_string'].'\') * '.$column2_weight.')
     + (MATCH(column3) AGAINST(\''.$_GET['search_string'].'\') * '.$column3_weight.')
     AS relevance'
    

    then

    'ORDER BY relevance'
    

提交回复
热议问题