目录
4.1、MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
4.2、concat_ws()函数, 表示concat with separator,即有分隔符的字符串连接
5、regexp_split_to_table和regexp_split_to_array
1、where
where 主要是用来简化 sql 语句中 where 条件判断,自动地处理 AND/OR 条件,and ,or等关键字可以多不可以没有,多了会自动去掉,少了会报错。
where用来包含多个if的,当多个if有一个成立的时候where会自动增加一个where关键字,并去掉if中多余的and,or等
使用where标签,在有查询条件中,可以自动添加上where子句;没有查询条件时,不会添加where子句。需值得注意的是:第一个if标签中的sql片段。可以不包含and,不过,写上and或者or也不错,系统会自动将多出的and去掉,但其他if中sql片段的and,必须要求写上否则SQL语句将拼接错误。
用法:
@select(
"select * from student " +
"<where>" +
" <if test="name !=null">" +
" name = #{name}" +
" </if>" +
" <if test="age > 100">" +
" or age > #{age}" +
" </if>" +
"</where>" +
"</script>" )
2、choose(when、otherwise)
if标签是与(and)的关系,只要test中的表达式为 true,就会执行 if 标签中的条件;而 choose 是或(or)的关系,并不想应用所有的条件,而只是想从多个选项中选择一个。
choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。
用法:
@select("<script>"+
"SELECT * "+
" FROM User u "+
"<where> "+
" <choose>"+
" <when test='username !=null '> "+
" u.username LIKE CONCAT('%', #{username}) "+
" </when > "+
" <when test='sex != null and sex != '' '> "+
" AND u.sex = #{sex}" +
" </when > "+
" <when test='birthday != null '> "+
" AND u.birthday = #{birthday} " +
" </when > "+
" <otherwise> "+
" </otherwise> "+
" </choose> "+
"</where> "+
"</script>")
3、any 和 all
这两个都是用于子查询的;any 是任意一个 ,all 是所有。
any表示有任何一个满足就返回true,all表示全部都满足才返回true 。
select * from student where 班级=’01’ and age > all (select age from student where 班级=’02’);
//就是说,查询出01班中,年龄大于 02班所有人的同学
//相当于
select * from student where 班级=’01’ and age > (select max(age) from student where 班级=’02’);
select * from student where 班级=’01’ and age > any (select age from student where 班级=’02’);
//就是说,查询出01班中,年龄大于 02班任意一个 的 同学
//相当于
select * from student where 班级=’01’ and age > (select min(age) from student where 班级=’02’);
4、concat 和 concat_ws
4.1、MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
mysql> select concat(‘11’,‘22’,null);
±-----------------------+
| concat(‘11’,‘22’,null) |
±-----------------------+
| NULL |
±-----------------------+
4.2、concat_ws()函数, 表示concat with separator,即有分隔符的字符串连接
mysql> select concat_ws(’,’,‘11’,‘22’,‘33’);
±------------------------------+
| concat_ws(’,’,‘11’,‘22’,‘33’) |
±------------------------------+
| 11,22,33 |
±------------------------------+
和concat不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL
mysql> select concat_ws(’,’,‘11’,‘22’,NULL);
±------------------------------+
| concat_ws(’,’,‘11’,‘22’,NULL) |
±------------------------------+
| 11,22 |
±------------------------------+
5、regexp_split_to_table和regexp_split_to_array
regexp_split_to_table、regexp_split_to_array这两个函数都是用来将字符串转换成格式化数据,一个是转换成结果集,一个是转换成数组。
regexp_split_to_table和regexp_split_to_array都是字符串分隔函数,可通过指定的表达式进行分隔。区别是regexp_split_to_table将分割出的数据转成行,regexp_split_to_array是将分隔的数据转成数组。
select regexp_split_to_table('a,b,c',',');
select regexp_split_to_table('/home/work/pg.sh','\/');
select regexp_split_to_table(coalesce('/home/work/pg.sh',''),E'\/');
select regexp_split_to_array('a,b,c',',');
select regexp_split_to_array('/home/work/pg.sh','\/');
select regexp_split_to_array(coalesce('/home/work/pg.sh',''),E'\/');
6、coalesce
coalesce是一个函数, (expression_1, expression_2, ...,expression_n)依次参考各参数表达式,遇到非null值即停止并返回该值。如果所有的表达式都是空值,最终将返回一个空值。使用COALESCE在于大部分包含空值的表达式最终将返回空值。
select coalesce(success_cnt, 1) from tableA
/*
当success_cnt 为null值的时候,将返回1,否则将返回success_cnt的真实值。
*/
select coalesce(success_cnt,period,1) from tableA
/*
当success_cnt不为null,那么无论period是否为null,都将返回success_cnt的真实值(因为success_cnt是第一个参数),
当success_cnt为null,而period不为null的时候,返回period的真实值。
只有当success_cnt和period均为null的时候,将返回1。
*/