sqlmysqlfull-text-searchmatchagainst

Using an overall WHERE clause when using UNIONS in sql


I am trying to return the MATCH() AGAINST() results against several tables using UNIONS, the only problem is some rows return a relevance of 0, I want to exclude these. After the unions is there a way to use 'WHERE relevance > 0'

Below is a bit of my 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

So is there anyway I can add WHERE relevance > 0 before the ORDER BY

many thanks


Solution

  • Try using a derived table

    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