6

我正在尝试使用 UNIONS 针对多个表返回 MATCH() AGAINST() 结果,唯一的问题是某些行返回的相关性为 0,我想排除这些。在工会之后有没有办法使用'WHERE相关性> 0'

下面是我的一些 SQL

SELECT pages.content AS search, page_info.url AS link, MATCH(pages.content) AGAINST('Wales') as relevance 
FROM page_content
LEFT JOIN pages ON (page_info.page = pages.id)

UNION

SELECT products_real.name AS search, products_real.event AS link, MATCH(products_real.name, products_real.description) AGAINST ('Wales') as relevance
FROM product_real

ORDER BY relevance DESC

那么无论如何我可以在 ORDER BY 之前添加 WHERE 相关性> 0

非常感谢

4

2 回答 2

4

尝试使用派生表

SELECT * 
FROM
  (SELECT 
    pages.content AS search, 
    page_info.url AS link, 
    MATCH(pages.content) AGAINST('Wales') as relevance  
  FROM page_content 
  LEFT JOIN pages ON (page_info.page = pages.id) 
  UNION 
  SELECT 
    products_real.name AS search, 
    products_real.event AS link, 
    MATCH(products_real.name, products_real.description) AGAINST ('Wales') as relevance 
  FROM product_real) myQuery
Where myQuery.relevance <> 0
ORDER BY myQuery.relevance DESC
于 2010-07-07T13:44:22.963 回答
2

您可以将其添加到每个查询或最后:

SELECT pages.content AS search, page_info.url AS link, MATCH(pages.content) AGAINST('Wales') as relevance 
FROM page_content
LEFT JOIN pages ON (page_info.page = pages.id)
WHERE relevance > 0

UNION

SELECT products_real.name AS search, products_real.event AS link, MATCH(products_real.name, products_real.description) AGAINST ('Wales') as relevance
FROM product_real
WHERE relevance > 0

ORDER BY relevance DESC



SELECT search, relevance FROM
(

SELECT pages.content AS search, page_info.url AS link, MATCH(pages.content) AGAINST('Wales') as relevance 
FROM page_content
LEFT JOIN pages ON (page_info.page = pages.id)

UNION

SELECT products_real.name AS search, products_real.event AS link, MATCH(products_real.name, products_real.description) AGAINST ('Wales') as relevance
FROM product_real
) D
WHERE relevance > 0
ORDER BY relevance DESC
于 2010-07-07T13:46:45.643 回答