在运行WHERE子句之前SELECT SELECT(Date)行?

时间:2011-10-07 00:33:40

标签: mysql sql aggregate-functions

我有这个查询在特定日期之前回显所有行。

  SELECT MAX(h.date), h.url
    FROM HISTORY h
   WHERE h.uid = '19'
     AND h.date < (SELECT MAX(t.date) 
                     FROM History t
                    WHERE t.url = 'canabalt.com' 
                      AND t.uid = '19')
GROUP BY h.url
ORDER BY MAX(h.date) DESC

我的问题是我必须只选择有max(Date)的行。

但是where子句消除了许多可能具有max(Date)行的行,然后在剩余的字段中查找max(Date)。

我怎样才能首先选择max(Date),然后再运行WHERE子句。

1 个答案:

答案 0 :(得分:2)

尝试将您的条件放在在Group By子句之后处理的Having子句中。

Select Url, Max(Date) As MaxDate
From History
Where UID = '19'
Group By Url
Having  Max( Date ) <   (
                        Select Max( H1.Date )
                        From History As H1
                        Where H1.URL = 'canabalt.com' 
                            And H1.UID = '19'
                        )
Order By Max( Date ) Desc