繁体   English   中英

MYSQL 列仅显示舍入值

[英]MYSQL column only showing rounded values

我在 mysql 中创建了一个表,如下所示:

create table scores (id tinyint, score tinyint);
insert into scores VALUES (1, 3.50), (2, 3.65),(3, 4.00),(4, 3.85);

但是当我显示值时,分数列将所有值显示为 4.00。 我已将“分数”列的数据类型更改为小数(10,2)、浮点数和双精度(10,2),但无济于事。

我该如何解决这个问题,

谢谢

如果不将原始数据重新插入表中,则无法解决此问题,因为有关分数的信息从未存储在数据库中。

显然,解决方案是使用适当的数据类型。 要么更改表,要么重新创建它,但无论如何你都必须再次插入数据:

方法 #1:更改表

create table scores (id tinyint, score tinyint);
insert into scores VALUES (1, 3.50), (2, 3.65),(3, 4.00),(4, 3.85);
ALTER TABLE scores MODIFY score decimal(10,2);
DELETE FROM scores;
insert into scores VALUES (1, 3.50), (2, 3.65),(3, 4.00),(4, 3.85);

方法#2:重新创建表

DROP TABLE scores;
create table scores (id tinyint, score decimal(10,2));
insert into scores VALUES (1, 3.50), (2, 3.65),(3, 4.00),(4, 3.85);

#1 优于 #2 的优势:您可以通过在 DELETE 语句中添加 WHERE 子句来删除和重新插入表数据的子集。 您不需要再次定义所有索引或约束。 #2优于#1的优势:在这种情况下很简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM