gpt4 book ai didi

ruby-on-rails - Rails 3 - mysql 返回与 postgresql 不同的结果

转载 作者:行者123 更新时间:2023-11-29 13:05:38 24 4
gpt4 key购买 nike

我正在尝试在我的查询中进行一些计算,我认为 postgresql 处理数学的方式与 mysql 不同,但我不确定区别在哪里。这是我的 Controller 查询:

Invoice.find(params[:id], :joins => :invoice_line_items, :select => "invoices.id, SUM(invoice_line_items.hours * invoice_line_items.rate) as subtotal, SUM(invoice_line_items.hours * invoice_line_items.rate) - (SUM(invoice_line_items.hours * invoice_line_items.rate) * (invoices.discount_percentage / 100)) as total", :group => "invoices.id")

在我的开发环境中,我使用的是 mysql,在我的生产环境中,我使用的是 postgresql。这些是我得到的输出。作为引用,discount_percentage 是 20。

MySQL:

 |  id  |  subtotal  |  total         |
----------------------------------------
| 21 | 570.0000 | 456.00000000 |

PostgreSQL:

 |  id  |  subtotal  |  total         |
----------------------------------------
| 9 | 570.0000 | 570.00000000 |

好像跟百分比和总数有关。有人有主意吗?顺便说一下,MySQL 结果就是我想要的。

最佳答案

Postgres 正在做整数除法。

Select                    MySQL   Postgres
----------------------- ----- --------
select 999 / 100 as a 9.99 9
select 999 / 100.0 as a 9.99 9.99

因此,将 x/100 更改为 x/100.0 以使它们的行为相同。

关于ruby-on-rails - Rails 3 - mysql 返回与 postgresql 不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13485966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com