我正在我的站点中开发一个带有分页的搜索功能来搜索产品名称和品牌名称。我正在使用此查询来获取用户的搜索请求:
SELECT *
from products
WHERE name = 'optimum nutrition'
OR brand = 'optimum nutrition'
OR name LIKE '%optimum%'
OR brand LIKE '%optimum%'
OR name LIKE '%nutrition%'
OR brand LIKE '%nutrition%'
我想首先展示在品牌名称和产品名称中都具有完整“最佳营养”的产品。我将如何实现这一目标?
如有任何建议,我们将不胜感激。
最佳答案
尝试:
SELECT *,
CASE WHEN (name = 'optimum nutrition' OR brand = 'optimum nutrition') THEN 1 ELSE 0 END AS full_match,
CASE WHEN (name LIKE '%optimum%' OR brand LIKE '%optimum%' OR name LIKE '%nutrition%' OR brand LIKE '%nutrition%') THEN 1 ELSE 0 END AS half_match
FROM products
WHERE (name = 'optimum nutrition' OR brand = 'optimum nutrition')
OR (name LIKE '%optimum%' OR brand LIKE '%optimum%' OR name LIKE '%nutrition%' OR brand LIKE '%nutrition%')
ORDER BY full_match, half_match
关于php - 在网站上使用分页搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676122/