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

correct invalid HAVING query

* GROUP BY value must appear in SELECT clause
* ("created_at < ?", 1.month.ago) wasn't a very good example for HAVING. you'd better use WHERE for such a simple condition
This commit is contained in:
Akira Matsuda 2011-06-29 08:29:34 +09:00
parent cfab51c819
commit 7d3a61a0e2

View file

@ -484,16 +484,16 @@ SQL uses the +HAVING+ clause to specify conditions on the +GROUP BY+ fields. You
For example:
<ruby>
Order.group("date(created_at)").having("created_at < ?", 1.month.ago)
Order.select("date(created_at) as ordered_date, sum(price) as total_price").group("date(created_at)").having("sum(price) > ?", 100)
</ruby>
The SQL that would be executed would be something like this:
<sql>
SELECT * FROM orders GROUP BY date(created_at) HAVING created_at < '2011-04-27'
SELECT date(created_at) as ordered_date, sum(price) as total_price FROM orders GROUP BY date(created_at) HAVING sum(price) > 100
</sql>
This will return single order objects for each day, but only those that are at least one month old.
This will return single order objects for each day, but only those that are ordered more than $100 in a day.
h3. Overriding Conditions