mysql IF语句,模糊检索

mysql IF语句,模糊检索

码农世界 2024-05-14 前端 63 次浏览 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 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, "" + mc.group() + "");
            }
            mc.appendTail(sb);
            return sb.toString();
        }
    

转载请注明来自码农世界,本文标题:《mysql IF语句,模糊检索》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,63人围观)参与讨论

还没有评论,来说两句吧...

Top