mysql IF语句,模糊检索

18 篇文章 1 订阅
2 篇文章 0 订阅
  • 使用MySQL IF语句完成条件检索
    IF(expr1,expr2,expr3),expr1如果满足条件就用expr2,否则用expr3
SELECT
	a.*,
	count(*) AS stdSum 
FROM
	idb_std_power_engin_v1 a 
WHERE
	1 = 1 
	AND (
	IF
		( 'KV' IS NOT NULL, a.NAME REGEXP 'KV', 1 = 1 ) 
	OR
	IF
		( 'KV' IS NOT NULL, a.description REGEXP 'KV', 1 = 1 ) 
	) 
AND
IF
	( 138 IS NOT NULL, a.country = 138, 1 = 1 ) 
GROUP BY
NAME 
	LIMIT 0,
	10

REGEXP 正则表达式,用于模糊匹配多个数据

/**
     * @description: 中英互换 正则表达查询
     * @author csb
     * @date: 2022/12/29 11:24
     */
    public static String suggestTermNameByRegular(String keyword,List<String> termName) {
        StringBuffer keywords = new StringBuffer();
        keywords.append(keyword+"|");
        if(null != termName && termName.size() > 0){
            termName.stream().forEach(a -> {
                keywords.append(a+'|');
            });
        }
        //去除最后的 |
        keywords.deleteCharAt(keywords.length() - 1);
        return String.valueOf(keywords);
    }
/**
     * @description: 关键字高亮
     * @author csb
     * @date: 2022/12/23 10:48
     */

    public static String IgnoreCaseReplace(String source, String patternstring) {
        Pattern p = Pattern.compile(patternstring, Pattern.CASE_INSENSITIVE);
        Matcher mc = p.matcher(source);
        StringBuffer sb = new StringBuffer();
        while (mc.find()) {
            mc.appendReplacement(sb, "<em>" + mc.group() + "</em>");
        }
        mc.appendTail(sb);
        return sb.toString();
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值