哪种 MySQL 类型最适合“价格”列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4397302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What MySQL type is most suitable for "price" column?
提问by Misha Moroshko
I have a price
column in products
table.
我price
在products
表中有一个列。
I wonder which MySQL type is the most suitable for this column. Is it DECIMAL
, FLOAT
, or something else ?
我想知道哪种 MySQL 类型最适合本专栏。难道DECIMAL
,FLOAT
或其他什么东西?
The price can be for example: 139.99
, 40
, 14.5
(2 digits after the decimal point, like in shops).
价格可以是例如:139.99
, 40
, 14.5
(小数点后 2 位数字,就像在商店中一样)。
Please advise.
请指教。
回答by beastofman
DECIMAL beacuse decimal value is stored precisely. E.g. DECIMAL(10, 2) will suit perfectly for prices not higher than 99999999,99. MySQL Docs reference
DECIMAL 因为十进制值被精确存储。例如 DECIMAL(10, 2) 将非常适合不高于 99999999,99 的价格。MySQL 文档参考
回答by Ali Nawaz
Field type "Decimal" will work best. Like:
字段类型“十进制”效果最好。喜欢:
`product_price` decimal(8, 2) NOT NULL,
It will store a price up to 999999.99
它将存储一个价格高达 999999.99
If you have high prices then you can use
如果你的价格很高,那么你可以使用
`product_price` decimal(12, 2) NOT NULL,
i.e. up to 9999999999.99
即最多 9999999999.99
回答by user207421
You should certainly use a decimal type for money. Never floating point, contrary to other answers. And using varchar prevents you doing calculations.
你当然应该使用十进制类型的钱。永远不会浮点,与其他答案相反。使用 varchar 可以防止您进行计算。