我有一个产品表,我正在使用简单的 like 子句进行搜索,我想搜索有边界和无边界的结果。
SELECT *
FROM products
WHERE title LIKE '%cold%'
例如,如果此查询返回以下行,
coldrum
rum cold
coldwine
cold wind
我想要这样的结果(先有边界,其余无边界)
cold wind
rum cold
coldrum
coldwine
嗯,实际的SQL是这样的,
select SQL_CALC_FOUND_ROWS distinct p.*, pd.*, m.*, i.image, if(s.status,
s.specials_new_products_price, null) as specials_new_products_price, if(s.status,
s.specials_new_products_price, if (pv.products_price, pv.products_price,
p.products_price)) as final_price from products p left join products_variants pv on
(p.products_id = pv.products_id and pv.is_default = 1) left join manufacturers m
using(manufacturers_id) left join specials s on (p.products_id = s.products_id) left
join products_images i on (p.products_id = i.products_id and i.default_flag = 1),
products_description pd, categories c, products_to_categories p2c where
p.products_status = 1 and p.products_id = pd.products_id and pd.language_id = 1 and
p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and
(pd.products_tags like '%gold%' or pd.products_name like '%gold%') order by
p.products_date_added desc limit 0, 20
最佳答案
这样试试
SELECT *
FROM products
WHERE title LIKE '%cold%'order by
case when title like 'cold %' then 0
when title like '% cold' then 1 else title end
关于带有和不带有单词边界的文本中的mysql搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25008384/