Re: HAVING confusion..

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-sql(at)hub(dot)org
Subject: Re: HAVING confusion..
Date: 1999-02-22 15:35:44
Message-ID: 8694.919697744@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"George Henry C. Daswani" <gdaswani(at)Vorlon(dot)odc(dot)net> writes:
> Basically I do
> SELECT u_id,expire_date FROM users HAVING expire_date >= (NOW())
> expire_date is of timestamp data type.
> and it keeps failing with
> ERROR: This is not a valid having query!

It's right: that isn't. You should be using WHERE not HAVING.

WHERE is for selecting individual rows to include or exclude. HAVING is
for selecting groups to list or not list when you have done a grouped
query (GROUP BY). That is, WHERE filters out rows before they are
collected into groups, and HAVING applies to the resulting groups.
So, HAVING without GROUP BY is incorrect.

I agree the error message could be a little more specific...

> basically I want to do the above that works with great with mysql

Evidently mysql is lax about the difference between WHERE and HAVING.
I wonder whether they get it right when you do have a GROUP BY clause?

regards, tom lane

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 1999-02-22 15:42:59 Re: [SQL] SQL query _slow_
Previous Message pierre 1999-02-22 14:46:44 questions on features