您使用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
-subquery爲返回一個布爾值並沒有什麼但布爾的。
如果你想從表2中計算等級,你必須在你的主查詢中添加一個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
如果只有一個最大表2中一個匹配行的,你可以逃脫不GROUP BY
條款,只是做了直接連接
SELECT t1.data1, MATCH (...) AGAINST (...) AS rank
FROM table1 t1
JOIN table2 t2 ON (MATCH (...) AGAINST (...))
ORDER BY rank DESC
的例子是真的做得很好,你值得擁有最好的答案!所以謝謝你! – user2302554 2013-04-20 18:33:10
TBH,我沒有看到這裏的子查詢的要求。我錯過了什麼嗎? – Hazzit 2013-04-20 18:41:08