如何在MySQL中强制将列别名设置为特定的数据类型?
为此,您可以使用CASE语句。让我们首先创建一个表-
mysql> create table DemoTable1505 -> ( -> Value integer unsigned, -> Status tinyint(1) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1505 values(20,0); mysql> insert into DemoTable1505 values(45,1);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1505;
这将产生以下输出-
+-------+--------+ | Value | Status | +-------+--------+ | 20 | 0 | | 45 | 1 | +-------+--------+ 2 rows in set (0.00 sec)
这是强制列别名为特定数据类型的查询-
mysql> select case status -> when 0 then cast(Value as signed)*1 -> when 1 then cast(Value as signed)*-1 -> end as AllValues from DemoTable1505;
这将产生以下输出-
+-----------+ | AllValues | +-----------+ | 20 | | -45 | +-----------+ 2 rows in set (0.00 sec)
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短