我正在编写一个查询来使用全文搜索来搜索mysql表:
select title,
MATCH(title,sidebarTxt,introTxt,fullTxt) AGAINST ('MY KEYWORDS' IN NATURAL LANGUAGE MODE) AS score
from tbl_news
where (status='published')
order by score DESC
以上查询工作正常。但是,如果我尝试在where子句中使用'score',MySql会在'where子句'中抛出错误“#1054 - 未知列'得分'。以下代码无效:
select title,
MATCH(title,sidebarTxt,introTxt,fullTxt) AGAINST ('MY KEYWORDS' IN NATURAL LANGUAGE MODE) AS score
from tbl_news
where status='published' and score > 0
order by score DESC
如果你能帮助我理解它,我将非常感激。
答案 0 :(得分:3)
如果score
是计算列,则无法在WHERE
子句中使用score
。您必须在其他HAVING
子句中设置它以在计算后应用条件:
SELECT title,
MATCH(title,sidebarTxt,introTxt,fullTxt)
AGAINST ('MY KEYWORDS' IN NATURAL LANGUAGE MODE) AS score
FROM tbl_news
WHERE status='published'
HAVING score > 0
ORDER BY score DESC;
测试用例(使用不同的表)。此处host
是一列,host2
是派生列。当然,列的推导和选择是假的,没什么意义:
mysql> use mysql;
Database changed
mysql> select host, UPPER(host) as host2 FROM user WHERE host = 'nottingham';
+------------+------------+
| host | host2 |
+------------+------------+
| nottingham | NOTTINGHAM |
+------------+------------+
1 row in set (0.00 sec)
mysql> select host, UPPER(host) as host2 FROM user WHERE host = 'nottingham' AND host2 = 'NOTTINGHAM';
ERROR 1054 (42S22): Unknown column 'host2' in 'where clause'
mysql> select host, UPPER(host) as 'host2' FROM user WHERE host = 'nottingham' AND host2 = 'NOTTINGHAM';
ERROR 1054 (42S22): Unknown column 'host2' in 'where clause'
mysql> select host, UPPER(host) as `host2` FROM user WHERE host = 'nottingham' AND host2 = 'NOTTINGHAM';
ERROR 1054 (42S22): Unknown column 'host2' in 'where clause'
mysql> select host, UPPER(host) as host2 FROM user WHERE host = 'nottingham' HAVING host2 = 'NOTTINGHAM';
+------------+------------+
| host | host2 |
+------------+------------+
| nottingham | NOTTINGHAM |
+------------+------------+
1 row in set (0.00 sec)
答案 1 :(得分:2)
问题是在查询完成运行之前,所有行都不知道score
值。因此,无法使用score
子句在WHERE
上应用条件。在计算完所有结果行之后执行ORDER BY
,因此使用它没有问题。
尝试使用HAVING
:
SELECT title, MATCH(title,sidebarTxt,introTxt,fullTxt) AGAINST ('MY KEYWORDS' IN NATURAL LANGUAGE MODE) AS score
FROM tbl_news
WHERE status='published'
HAVING score > 0
ORDER BY score DESC
HAVING
子句的行为几乎与WHERE
一样,差异是>>在计算结果行之后执行。此时,每个结果行都知道score
的值,并且可以应用条件。
文档:
Fulltext search:查看示例,他们使用HAVING
Syntax of SELECT:查看有关HAVING
用法
答案 2 :(得分:0)
编辑:错误
尝试将单引号添加到:as 'score'
...它似乎无法识别您指定该列的名称。
答案 3 :(得分:0)
这很可能是50%噪音的情况。当你有一个50%或更多行的单词时,mysql认为它是一个 noise 单词,并在得分(因此排名)时忽略它。这仅适用于NATURAL LANGUAGE MODE
选项。
不要挑剔,但它应该完美地运作,没有任何引号:
mysql> select * from table1;
+---------+------+------+-------------+
| autonum | ID | name | metavalue |
+---------+------+------+-------------+
| 1 | 1 | Rose | Drinker |
| 2 | 1 | Rose | Nice Person |
| 3 | 1 | Rose | Runner |
| 4 | 2 | Gary | Player |
| 5 | 2 | Gary | Funny |
| 6 | 2 | Gary | NULL |
| 7 | 2 | Gary | Smelly |
+---------+------+------+-------------+
7 rows in set (0.00 sec)
mysql> select ID, group_concat(coalesce(metavalue,'Empty')) as Test from table1 group by ID order by Test;
+------+----------------------------+
| ID | Test |
+------+----------------------------+
| 1 | Drinker,Nice Person,Runner |
| 2 | Player,Funny,Empty,Smelly |
+------+----------------------------+
2 rows in set (0.01 sec)
mysql> select ID, group_concat(coalesce(metavalue,'Empty')) as Test from table1 group by ID order by Test desc;
+------+----------------------------+
| ID | Test |
+------+----------------------------+
| 2 | Player,Funny,Empty,Smelly |
| 1 | Drinker,Nice Person,Runner |
+------+----------------------------+
2 rows in set (0.00 sec)
Server version: 5.5.24-0ubuntu0.12.04.1 (Ubuntu)
(这种查询在我的Windows 5.1.x安装工作中完全正常)