1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

correct invalid GROUP BY query

GROUP BY value must appear in SELECT clause
This commit is contained in:
Akira Matsuda 2011-06-29 08:04:11 +09:00
parent 6a1803a14f
commit cfab51c819

View file

@ -466,7 +466,7 @@ To apply a +GROUP BY+ clause to the SQL fired by the finder, you can specify the
For example, if you want to find a collection of the dates orders were created on: For example, if you want to find a collection of the dates orders were created on:
<ruby> <ruby>
Order.group("date(created_at)").order("created_at") Order.select("date(created_at) as ordered_date, sum(price) as total_price").group("date(created_at)")
</ruby> </ruby>
And this will give you a single +Order+ object for each date where there are orders in the database. And this will give you a single +Order+ object for each date where there are orders in the database.
@ -474,7 +474,7 @@ And this will give you a single +Order+ object for each date where there are ord
The SQL that would be executed would be something like this: The SQL that would be executed would be something like this:
<sql> <sql>
SELECT * FROM orders GROUP BY date(created_at) ORDER BY created_at SELECT date(created_at) as ordered_date, sum(price) as total_price FROM orders GROUP BY date(created_at)
</sql> </sql>
h3. Having h3. Having