问题一:sql输出指定格式的时间日期
sql to_char函数直接输出指定日期格式:
//将时间日期按照指定的格式输出,得到的是字符串,而非date类型。
to_char(createtime, 'yyyy-MM-dd HH:mm:ss')
其他用途:
// 将布尔型、整型或者浮点型数值转为对应的字符串表示
to_char(boolean)
to_char(true)返回’TRUE’
to_char(bigint)
to_char(123)返回’123’
to_char(double)
to_char(1.23)返回’1.23’
参数类型:
单参数的to_char可以接受datetime类型,布尔型,整型或者浮点型输入,其它类型抛异常。对应值的字符串表示,如果输入为NULL,返回NULL。
问题二:foreach函数
当需要sql中遍历数组,格式如下
// 对应list为list名称
<foreach collection="list" index="index" item="list" open="(" separator="," close=")">
#{list}
</foreach>
问题三:先根据一个字段优先排序,再根据某一个排序字段进行排序
今天的话,有一个需求,评论列表优先显示自己的,再根据评论时间进行倒叙排序,sql的话写在下面
// 优先显示创建人工号为123的,再根据创建时间进行倒叙排序
select 字段 from 表名
where 条件 = ?
order by
case when creator_code = '123'
then 1 else 0 end desc,create_time desc
这个case when大概的意思的话,当创建人工号是123的话,就是1,其他的话就是0,1和0倒叙排,所以123优先显示;然后是逗号分割,加入第二个条件进行排序。
问题四
Mybatis动态sql之choose函数
<choose>
<when test="client != null and client == 'PC'">
,group_concat(DISTINCT concat(m.module_name, '-PC端-', r.role_name)) as roles_str
</when>
<when test="client != null and client == 'APP'">
,group_concat(DISTINCT concat(am.module_name, '-APP端-', ar.role_name)) as roles_str
</when>
<otherwise>
,concat_ws(',', group_concat(DISTINCT concat(m.module_name, '-PC端-', r.role_name)), group_concat(DISTINCT concat(am.module_name, '-APP端-', ar.role_name))) as roles_str
</otherwise>
</choose>