我有这样的查询:
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
。
答案 0 :(得分: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