为什么无论是通过浏览器还是通过控制台ActiveRecord
总是返回?-1
SQL 语句:
SELECT category.categoryname, sum(lineitems.qty) as totalSales,
sum(lineitems.qty*size.sizeship) as volume,
sum(lineitems.qty*lineitems.purcprice) AS totmerchandise FROM category, products,
orders, shipments, lineitems, size WHERE
category.categoryid = products.categoryid AND products.productid =
lineitems.productid AND lineitems.posshipid = shipments.posshipid AND
shipments.posorderid = orders.posorderid AND size.sizeid = products.size AND
category.categoryid NOT IN (77,79) AND orders.orderstatus in
(1,4,5) AND orders.ordercomplete = 1 AND numbotincase > 0 AND orders.date >=
'20130501' AND orders.date < '20130531' GROUP BY
category.categoryname ORDER BY category.categoryname`
当我在 Microsoft SQL Server 中执行此操作时,它会加载一堆记录。但是,在 Rails 中,当我尝试这样做时:
query_for_category_bottles_volume_totalsales = "SELECT category.categoryname,
sum(lineitems.qty) as totalSales, sum(lineitems.qty*size.sizeship) as volume,
sum(lineitems.qty*lineitems.purcprice) AS totmerchandise FROM category, products,
orders, shipments, lineitems, size WHERE category.categoryid = products.categoryid
AND products.productid = lineitems.productid AND lineitems.posshipid =
shipments.posshipid AND shipments.posorderid = orders.posorderid AND
size.sizeid = products.size AND category.categoryid NOT IN (77,79) AND
orders.orderstatus in (1,4,5) AND orders.ordercomplete = 1 AND numbotincase > 0
AND orders.date >= '20130501' AND orders.date < '20130531' GROUP BY
category.categoryname ORDER BY category.categoryname"
category_bottles_volume_totalsales = ActiveRecord::Base.connection.execute(query_for_category_bottles_volume_totalsales)
这不仅发生在这个查询中,而且像这样简单的事情也会发生:ActiveRecord::Base.connection.execute("select * from orders where id = 987;")
. 但是,当我这样做时,在控制台中Order.find(987)
会返回一条记录。