如何在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)
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便