使用 MySQL 可以将双精度和日期存储在 VARCHAR 中吗?
mysqlmysqli database
可以,您可以将双精度和日期存储在 VARCHAR 中。让我们首先创建一个表 −
mysql> create table DemoTable -> ( -> Amount varchar(20), -> JoiningDate varchar(20) -> ); Query OK, 0 rows affected (0.96 sec)
使用 insert 命令在表中插入一些记录。We are inserting varchar double and date values −
mysql> insert into DemoTable values('150.50','Oct 10,2019'); Query OK, 1 row affected (0.91 sec) mysql> insert into DemoTable values('173.45','Sept 11,2018'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('173.90','Jan 01,2017'); Query OK, 1 row affected (0.16 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable;
这将产生以下输出 −
+--------+--------------+ | Amount | JoiningDate | +--------+--------------+ | 150.50 | Oct 10,2019 | | 173.45 | Sept 11,2018 | | 173.90 | Jan 01,2017 | +--------+--------------+ 3 rows in set (0.00 sec)
现在,我们将 VARCHAR 记录存储为 DOUBLE 和 DATE 类型 −
mysql> select Amount+0.0 as DoubleValue,str_to_date(JoiningDate,'%M %d,%Y') as Date from DemoTable;
这将产生以下输出 −
+-------------+------------+ | DoubleValue | Date | +-------------+------------+ | 150.5 | 2019-10-10 | | 173.45 | 2018-09-11 | | 173.9 | 2017-01-01 | +-------------+------------+ 3 rows in set (0.04 sec)