订单结果全文

时间:2013-04-20 16:51:45

标签: mysql sql

按照我的查询:

SELECT data1
FROM table1 t1
WHERE EXISTS (
SELECT id,
MATCH (
...
)
AGAINST (
...
) AS rank
FROM table2 t2
WHERE MATCH (
...
)
AGAINST (
....
)
) ORDER BY rank DESC

问题在于我不认识排名在外的位置,实际上我看到以下消息:

  

#1054 - 'order clause'中的未知列'rank'

2 个答案:

答案 0 :(得分:0)

您正在使用WHERE EXISTS和子查询。

SELECT data1 FROM table1 t1 WHERE EXISTS (
    SELECT id,
    MATCH (...) AGAINST (...) AS rank
    FROM table2 t2
    WHERE MATCH (...) AGAINST (...) 
) ORDER BY rank DESC

在此上下文中,MySQL ignores选定的字段。它还可以阅读:

SELECT data1 FROM table1 t1 WHERE EXISTS (
    SELECT 42
    FROM table2 t2
    WHERE MATCH (...) AGAINST (...) 
) ORDER BY rank DESC

它会起作用。

WHERE EXISTS - 子查询看作是返回一个布尔值,除了那个布尔值。

如果您希望从table2计算排名,则必须在主查询中添加JOIN(可能还有GROUP BY子句。)

编辑:示例

很可能您不需要WHERE EXISTS子句。 (你的问题太缺乏确定,所以这是标准情况)

SELECT t1.data1, MATCH (...) AGAINST (...) AS rank
FROM table1 t1 
JOIN table2 t2 ON (MATCH (...) AGAINST (...))
GROUP BY t1.id, t1.data, rank
ORDER BY rank DESC

如果table2中只有一个最多的一个匹配行,那么您可以在没有GROUP BY子句的情况下离开,只需进行直接连接

SELECT t1.data1, MATCH (...) AGAINST (...) AS rank
FROM table1 t1 
JOIN table2 t2 ON (MATCH (...) AGAINST (...))
ORDER BY rank DESC

答案 1 :(得分:0)

在SQL语句中,您只能引用主from子句中的表/子查询中的列。您的子查询位于where子句中。

您需要将其重写为join。如果{2}在表2中是唯一的,那么这将起作用:

id

否则,您需要考虑重复项:

select data1
from table1 t1 join
     (SELECT id, MATCH (...) AGAINST (...) AS rank
      FROM table2 t2
      WHERE MATCH (...) AGAINST (....)
     ) t2
     on t1.id = t2.id
order by t2.rank