sql开发问题

问题一:sql输出指定格式的时间日期

sql to_char函数直接输出指定日期格式:

//将时间日期按照指定的格式输出,得到的是字符串,而非date类型。
to_char(createtime, 'yyyy-MM-dd HH:mm:ss')

其他用途:

// 将布尔型、整型或者浮点型数值转为对应的字符串表示 
to_char(boolean)
to_char(true)返回’TRUEto_char(bigint)
to_char(123)返回’123to_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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值