CONCAT_WS 此函数是用来讲多个字段的内容拼接成一个字符串 进行搜索
此表 mall_product ,多个关键字从这 title, subtitle, tag 三个字段或者跟多字段。
进行搜索时的场景(单个字段 或 多个字段都可兼容):
匹配关键词的多少来排序匹配关键词越多的文章越靠前
SELECT *,
(
(IF( CONCAT_WS(" ", title, subtitle, tag) LIKE "%车%", 1, 0))
+ (IF( CONCAT_WS(" ", title, subtitle, tag) LIKE "%摩托%", 1, 0))
+ (IF( CONCAT_WS(" ", title, subtitle, tag) LIKE "%红色%", 1, 0))
+ (IF( CONCAT_WS(" ", title, subtitle, tag) LIKE "%美国%", 1, 0))
+ (IF( CONCAT_WS(" ", title, subtitle, tag) LIKE "%2006%", 1, 0))
) AS keyweight
FROM mall_product WHERE
CONCAT_WS(" ", title, subtitle, tag) REGEXP "车|摩托|红色|美国|2006"
ORDER BY keyweight DESC
此表 mall_product ,多个关键字从这 name 单个字段进行搜索时的场景:
匹配关键词的多少来排序匹配关键词越多的文章越靠前
SELECT *,
(
(IF( name LIKE "%货车%", 1, 0))
+ (IF( name LIKE "%车%", 1, 0))
+ (IF( name LIKE "%红色%", 1, 0))
+ (IF( name LIKE "%美国%", 1, 0))
+ (IF( name LIKE "%2006%", 1, 0))
) AS keyweight
FROM mall_product WHERE
name REGEXP "车|货车|美国|2006"
ORDER BY keyweight DESC