Rails 3 – mysql返回的结果与postgresql不同

我试图在我的查询中做一些计算,我认为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的结果就是我想要的。

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以使它们的行为相同。