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

时间:2022-12-25 22:50:49

I have this query that echoes all rows prior a particular date.

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

  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

My problem is that I must only select rows that have max(Date).

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

But the where clause eliminates a number of rows that may have the max(Date) row and then looks for max(Date) in the remaining fields.

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

How can I first SELECT max(Date) and only afterwards run the WHERE clauses.

我怎样才能首先选择最大值(日期),然后再运行WHERE子句。

1 个解决方案

#1


2  

Try placing your criteria in the Having clause which is processed after the Group By clause.

尝试将您的条件放在在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

#1


2  

Try placing your criteria in the Having clause which is processed after the Group By clause.

尝试将您的条件放在在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