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

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;

扫描二维码关注公众号,回复: 10288024 查看本文章

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

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

发布了1266 篇原创文章 · 获赞 275 · 访问量 290万+

猜你喜欢

转载自blog.csdn.net/zhouzhiwengang/article/details/105183730