我从雄辩的查询中得到了一些结果,我想按最佳匹配对它们进行排序。我怎样才能在 laravel eloquent 中做到这一点?
在这里我找到了一些 SQL 解决方案,但我无法在 eloquent 构建器中使用它。
SELECT TOP 5 *
FROM Products
WHERE ProductCode LIKE '%200%'
ORDER BY CHARINDEX('200', ProductCode, 1), ProductCode
https://bytutorial.com/blogs/tsql/how-to-order-the-sql-query-result-by-best-match-keyword-search https://bytutorial.com/blogs/tsql/how-to-order-the-sql-query-result-by-best-match-keyword-search
是的,按照建议,您可以使用orderByRaw()
所以你的查询将被转换为:
$result = Product::where("ProductCode", "LIKE", '%200%')
->orderByRaw('CHARINDEX('200', ProductCode, 1) DESC, ProductCode ASC')
->get();
这里,假设模型名称是Product
餐桌用products
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)