前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PostgreSQL在进行除法时要注意

PostgreSQL在进行除法时要注意

作者头像
翎野君
发布2023-09-06 13:48:14
4130
发布2023-09-06 13:48:14
举报
文章被收录于专栏:翎野君翎野君

背景

整型除以整型,正常情况下当然得到的应该也是整型。数据库也是这么干的。

但是在数据库应用中,通常业务的需求是得到NUMERIC,不能直接把小数干掉。

数据库的行为给用户带来了诸多不便,例如1除以2,如果是整型除法会得到0,然而用户可能要的是0.5。

那么怎么处理呢?

1、建议用户使用float8, money, numeric来存储这种数值。

2、或者在除法时,对操作数进行转换。

操作符

代码语言:javascript
复制
postgres=# \do+ /  
                                                     List of operators  
   Schema   | Name |  Left arg type   |  Right arg type  |   Result type    |   Function    |         Description            
------------+------+------------------+------------------+------------------+---------------+------------------------------  
 pg_catalog | /    | bigint           | bigint           | bigint           | int8div       | divide  
 pg_catalog | /    | bigint           | integer          | bigint           | int84div      | divide  
 pg_catalog | /    | bigint           | smallint         | bigint           | int82div      | divide  
 pg_catalog | /    | box              | point            | box              | box_div       | divide box by point (scale)  
 pg_catalog | /    | circle           | point            | circle           | circle_div_pt | divide  
 pg_catalog | /    | double precision | double precision | double precision | float8div     | divide  
 pg_catalog | /    | double precision | real             | double precision | float84div    | divide  
 pg_catalog | /    | integer          | bigint           | bigint           | int48div      | divide  
 pg_catalog | /    | integer          | integer          | integer          | int4div       | divide  
 pg_catalog | /    | integer          | smallint         | integer          | int42div      | divide  
 pg_catalog | /    | interval         | double precision | interval         | interval_div  | divide  
 pg_catalog | /    | money            | bigint           | money            | cash_div_int8 | divide  
 pg_catalog | /    | money            | double precision | money            | cash_div_flt8 | divide  
 pg_catalog | /    | money            | integer          | money            | cash_div_int4 | divide  
 pg_catalog | /    | money            | money            | double precision | cash_div_cash | divide  
 pg_catalog | /    | money            | real             | money            | cash_div_flt4 | divide  
 pg_catalog | /    | money            | smallint         | money            | cash_div_int2 | divide  
 pg_catalog | /    | numeric          | numeric          | numeric          | numeric_div   | divide  
 pg_catalog | /    | path             | point            | path             | path_div_pt   | divide (rotate/scale path)  
 pg_catalog | /    | point            | point            | point            | point_div     | divide points (scale/rotate)  
 pg_catalog | /    | real             | double precision | double precision | float48div    | divide  
 pg_catalog | /    | real             | real             | real             | float4div     | divide  
 pg_catalog | /    | smallint         | bigint           | bigint           | int28div      | divide  
 pg_catalog | /    | smallint         | integer          | integer          | int24div      | divide  
 pg_catalog | /    | smallint         | smallint         | smallint         | int2div       | divide  
(25 rows)  

显示转换

int 转成 numeric或者float8后再除。

例子

代码语言:javascript
复制
postgres=# select 1/2;  
 ?column?   
----------  
        0  
(1 row)  
显示转换任意操作数

postgres=# select 1/2.0;  
 ?column?   
----------  
      0.5  
(1 row)  

postgres=# select 1/2::float8;  
 ?column?   
----------  
      0.5  
(1 row)  
  
  
postgres=# select 1/2::numeric;  
        ?column?          
------------------------  
 0.50000000000000000000  
(1 row)  

本篇文章如有帮助到您,请给「翎野君」点个赞,感谢您的支持。

首发链接: https://cloud.tencent.com/developer/article/2324752

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 操作符
    • 显示转换
      • 例子
      相关产品与服务
      数据库
      云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档