douzhongjiu2263 2014-07-27 19:25
浏览 130
已采纳

MATCH ... AGAINST没有得出确切的结果

I'm working for the first time with MATCH...AGAINST in php sql but there is one bothering me and I can't figure out how to fix it. This is my code:

SELECT * FROM m_artist WHERE match(artist_name) against('". $_POST['article_content'] ."' IN BOOLEAN MODE)

And this is $_POST['article_content']:

Wildstylez Brothers Yeah Frontliner Waveliner

Now my output should be: Wildstylez, Frontliner and Waveliner cause that's in my database. And I do but besides that I also get the Vodka Brothers, 2 Brothers of Hardstyle and more cause of the word brothers. How do I fix that SQL only selects the literal match?

  • 写回答

1条回答 默认 最新

  • douyouqian8550 2014-07-27 20:08
    关注

    Full-text search actually is a quite misleading name: you can search the full text by your query (like google does) but it won't guarantee you, that the full text equals your query.

    So, according to documentation on Boolean Full-Text Searches your input Wildstylez Brothers Yeah Frontliner Waveliner is interpreted as artist_name contains (at least) one of Wildstylez, Brothers, Yeah, Frontliner and Waveliner as word. This is why you get e.g. the Vodka Brothers, which contains Brothers. For google-like purposes this is just what you want, as you want to get details on something you only know part of as in show me articles on music.

    You probably want to use

    artist_name LIKE '%name_part1%' OR artist_name LIKE '%name_part2%' ...
    

    or

    artist_name IN ('exact_name1', 'exact_name2', ...)
    

    simpliest case would be doing something like

    $names = explode(' ', $_POST['article_content']);
    $name_searches = array_map(function($a) {return 'artist_name = \''.mysql_real_escape_string($a).'\'';}, $names);
    $sql = "SELECT * FROM m_artist WHERE ".implode(" OR ", $name_searches);
    

    but you would loose the ability to find 2 Brothers of Hardstyle as the name itself contains a space.

    Another approach can be to prefix all words by '+' and stick to MATCH() AGAINST() and you will find only artists which include every word given.

    Please provide more context if this is not what you are looking for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow