1

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

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 结果。

4

1 回答 1

2

Postgres 正在做整数除法。

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

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

于 2012-11-21T03:44:32.173 回答