0
点赞
收藏
分享

微信扫一扫

mysql IF语句,模糊检索

艾晓雪 2024-05-26 阅读 7
mysqlsql
  • 使用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();
    }
举报

相关推荐

0 条评论