MySQL 转换函数cast 和convert,升序和降序

小虾米 阅读:808 2021-03-31 16:51:21 评论:0

Cast 函数和Convert 函数简单功能描述:用来获取一个类型的值,并产生另一个类型的值。

语法规则:

CAST(value as type) = CAST(xxx AS 类型)

CONVERT(value, type) = CONVERT(xxx,类型)

Cast 函数转换类型限制(可以转换类型表格如下):

数据类型 备注说明
 BINARY 二进制
CHAR 字符类型
DATE 日期
TIME 时间
DATETIME  日期时间
DECIMAL  浮点数
SIGNED 整数
UNSIGNED 无符号整数

 

MyBaties 3 使用Cast 函数,判断流水号是否在指定的范围内:

    <if test="boxIdBegin != null and boxIdBegin !=''"> 
		<if test="boxIdEnd == null or boxIdEnd ==''"> 
			and cast(ucas_box_info.box_id as signed) &gt;= cast(#{boxIdBegin} as signed) 
		</if> 
	</if> 
	<if test="boxIdBegin == null or boxIdBegin ==''"> 
		<if test="boxIdEnd != null and boxIdEnd !=''"> 
			and cast(ucas_box_info.box_id as signed) &lt;= cast(#{boxIdEnd} as signed) 
		</if> 
	</if> 
	<if test="boxIdBegin != null and boxIdBegin !=''"> 
		<if test="boxIdEnd != null and boxIdEnd !=''"> 
			and ucas_box_info.box_id between cast(#{boxIdBegin} as signed) and cast(#        {boxIdEnd} as signed) 
		</if> 
	</if>

Convert 函数的使用与上类似。

 

order by 升序和降序排列显示
SQL 语句中, asc 指定列按升序排列,desc 指定列按降序排列。

排序子句语法:order by 列名 asc / desc

例表格:tablename

1、 按列 c2 的升序排列
select * from tablename order by c2 asc;

2、 按列 c2 的降序排列
select * from tablename order by c2 desc;

3、 复合排序,先按列 c1 升序排列,再按 c2 降序排列
select * from tablename order by c1 asc, c2 desc;
 

 

 

标签:mysql
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号