4

我有一个这样的查询:

SELECT products.* FROM products
WHERE products.code = 'search word' OR products.id IN(SELECT product_names.product_id 
                     FROM product_names 
                     WHERE MATCH (product_names.name) 
                     AGAINST ('+search* +word*' IN BOOLEAN MODE) 
                     ORDER BY product_names.sorting_field)
ORDER BY ???

我希望主查询的顺序与WHERE IN(). 我似乎无法使用FIELD,或者FIND_IN_SET因为我不知道我从MATCH AGAINST.

我尝试了诸如此类的东西,ORDER BY FIELD(products.id, product_names.product_id)但我不知道如何使它工作。

实际上,我的查询更长更复杂,这是它的一个伪版本。我不能MATCH AGAINST在子查询之外做。

4

1 回答 1

3
SELECT   products.*
FROM     products LEFT JOIN (
  SELECT product_id, sorting_field
  FROM   product_names 
  WHERE  MATCH (product_names.name) AGAINST ('+search* +word*' IN BOOLEAN MODE) 
) t ON products.id = t.product_id
WHERE    products.code = 'search word' OR t.product_id IS NOT NULL
ORDER BY t.sorting_field
于 2012-10-11T14:15:09.917 回答