是的,您可以在VARCHAR中存储双精度和日期。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Amount varchar(20), -> JoiningDate varchar(20) -> );
使用insert命令在表中插入一些记录。我们正在插入varchar double和date值-
mysql> insert into DemoTable values('150.50','Oct 10,2019'); mysql> insert into DemoTable values('173.45','Sept 11,2018'); mysql> insert into DemoTable values('173.90','Jan 01,2017');
使用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)