Mysql - 全文搜索 - 匹配值在哪里?

时间:2013-11-19 18:35:03

标签: mysql full-text-search where

我有这样的查询

SELECT id, search, MATCH(search) AGAINST('+iron +maiden') m FROM mp3 WHERE MATCH(search) AGAINST('+iron +maiden') ORDER BY m DESC;

工作完美,结果分类,每个人都很好。 然后我试图将我的结果限制为一些最佳匹配。我注意到m介于4到20之间(并非总是如此) 我想:

SELECT id, search, MATCH(search) AGAINST('+iron +maiden') m FROM mp3 WHERE m > 10 AND MATCH (search) AGAINST ('+iron +maiden') ORDER BY m DESC;

的Bam! ERROR 1054(42S22):'where子句'中的未知列'm'

现在我知道它不起作用,但我不知道为什么,也许我累了或者什么...... 所以我想问你:

  1. 为什么不起作用?
  2. 有类似的工作方案吗? (仅限sql,我知道我可以在以后过滤结果)
  3. 干杯

    修改 所以@alko建议我做一些基准测试,看起来很有希望:

    Match against within inner select (without WHERE m > 10 so resultset is identical as in match against below):
    SELECT id, search, m FROM (SELECT id, search, MATCH(search) AGAINST('+metallica') m    FROM mp3 WHERE MATCH(search) AGAINST('+metallica')) matched_mp3 ORDER BY m DESC;
    2818 rows in set (0.03 sec)
    SELECT id, search, m FROM (SELECT id, search, MATCH(search) AGAINST('+dead +weather') m FROM mp3 WHERE MATCH(search) AGAINST('+dead +weather')) matched_mp3 ORDER BY m DESC;
    6968 rows in set (0.10 sec)
    SELECT id, search, m FROM (SELECT id, search, MATCH(search) AGAINST('+led +zeppelin') m FROM mp3 WHERE MATCH(search) AGAINST('+led +zeppelin')) matched_mp3 ORDER BY m DESC;
    1381 rows in set (0.02 sec)
    SELECT id, search, m FROM (SELECT id, search, MATCH(search) AGAINST('+lana +del +ray +paradise') m FROM mp3 WHERE MATCH(search) AGAINST('+lana +del +ray +paradise')) matched_mp3 ORDER BY m DESC;
    7447 rows in set (0.11 sec)
    
    Match against:
    SELECT id, search, MATCH(search) AGAINST('+metallica') m FROM mp3 WHERE MATCH(search) AGAINST('+metallica') ORDER BY m DESC;
    2818 rows in set (0.03 sec)
    SELECT id, search, MATCH(search) AGAINST('+dead +weather') m FROM mp3 WHERE MATCH(search) AGAINST('+dead +weather') ORDER BY m DESC;
    6968 rows in set (0.10 sec)
    SELECT id, search, MATCH(search) AGAINST('+led +zeppelin') m FROM mp3 WHERE MATCH(search) AGAINST('+led +zeppelin') ORDER BY m DESC;
    1381 rows in set (0.01 sec)
    SELECT id, search, MATCH(search) AGAINST('+lana +del +ray +paradise') m FROM mp3 WHERE MATCH(search) AGAINST('+lana +del +ray +paradise') ORDER BY m DESC;
    7447 rows in set (0.12 sec)
    
    Match against within inner select (with WHERE m > 10)
    SELECT id, search, m FROM (SELECT id, search, MATCH(search) AGAINST('+metallica') m FROM mp3 WHERE MATCH(search) AGAINST('+metallica')) matched_mp3 WHERE m > 10 ORDER BY m DESC;
    8 rows in set (0.02 sec)
    SELECT id, search, m FROM (SELECT id, search, MATCH(search) AGAINST('+dead +weather') m FROM mp3 WHERE MATCH(search) AGAINST('+dead +weather')) matched_mp3 WHERE m > 10 ORDER BY m DESC;
    46 rows in set (0.08 sec)
    SELECT id, search, m FROM (SELECT id, search, MATCH(search) AGAINST('+led +zeppelin') m FROM mp3 WHERE MATCH(search) AGAINST('+led +zeppelin')) matched_mp3 WHERE m > 10 ORDER BY m DESC;
    1070 rows in set (0.01 sec)
    SELECT id, search, m FROM (SELECT id, search, MATCH(search) AGAINST('+lana +del +ray +paradise') m FROM mp3 WHERE MATCH(search) AGAINST('+lana +del +ray +paradise')) matched_mp3 WHERE m > 10 ORDER BY m DESC;
    532 rows in set (0.06 sec)
    

1 个答案:

答案 0 :(得分:1)

它不会独立于全文搜索,例如

select concat(name, ' ', surname) as full_name
  from user
 where full_name = 'john smith'

最终会出现同样的错误。原因是selectwhere子句之前被评估。因此,当评估where子句时,尚未存在别名表达式。可能最简单的方法是用内部选择包装:

SELECT * 
  FROM (SELECT id, 
                search, 
                MATCH(search) AGAINST('+iron +maiden') m 
         FROM mp3) matched_mp3
  WHERE m > 10 
  ORDER BY m DESC;