MySQL 如何在SELECT语句中使用CAST函数
MySQL中的CAST()函数将任何类型的值转换为具有指定类型的值。让我们首先创建一个表 –
mysql> create table castFunctionDemo
-> (
-> ShippingDate date
-> );
Query OK, 0 rows affected (0.74 sec)
以下是使用插入命令在表中插入一些记录的查询语句 –
mysql> insert into castFunctionDemo values('2019-01-31');
Query OK, 1 row affected (0.20 sec)
mysql> insert into castFunctionDemo values('2018-07-12');
Query OK, 1 row affected (0.16 sec)
mysql> insert into castFunctionDemo values('2016-12-06');
Query OK, 1 row affected (0.16 sec)
mysql> insert into castFunctionDemo values('2017-08-25');
Query OK, 1 row affected (0.19 sec)
以下是使用select语句显示表中所有记录的查询语句 –
mysql> select * from castFunctionDemo;
这将产生以下输出 –
+--------------+
| ShippingDate |
+--------------+
| 2019-01-31 |
| 2018-07-12 |
| 2016-12-06 |
| 2017-08-25 |
+--------------+
4 rows in set (0.00 sec)
以下是在MySQL select语句中正确使用cast()函数的查询语句 –
mysql> select CAST(ShippingDate AS CHAR(12)) as Conversion FROM castFunctionDemo;
这将产生以下输出 –
+------------+
| Conversion |
+------------+
| 2019-01-31 |
| 2018-07-12 |
| 2016-12-06 |
| 2017-08-25 |
+------------+
4 rows in set (0.00 sec)
阅读更多:MySQL 教程