1.类型转换函数,CAST(exp as type),参数exp是表达式,参数type是要转换的类型。
2.日期类型的相减,包括日期的相减和时间的相减,datediff(val1,val2),timediff(val1,val2)。
实际应用:select *,case when datediff(CAST(close_time as datetime),CAST(open_time as datetime)) = 0
then timediff(CAST(close_time as datetime),CAST(open_time as datetime))
when datediff(CAST(close_time as datetime),CAST(open_time as datetime)) > 0
then datediff(CAST(close_time as datetime),CAST(open_time as datetime)) end positiontime from statement
where status = '3' and user_id = 'cmbro' and account = '112274' order by open_time limit 100;
代码说明:close_time和open_time进行类型转换后按日期相减,如果结果是0,表示close_time和open_time在同一天,这时取
两个值按时间相减的结果。如果close_time和open_time按日期相减的结果大于0,就取按日期相减的结果。