开发者

MySQL Select Dates in the Past: Problem with MAX and GROUP BY

So, the query I'm trying to make looks like this right now:

SELECT `jid`, MAX(`start`) as `start` FRO开发者_如何学JAVAM `dates` WHERE `start` < NOW() GROUP BY `jid`

The table dates basically holds a DATETIME value start associated with a job.

As you probably guessed, jid is the id of a job, stored in another table.

A job can have many dates

What I'm trying to accomplish is to find out what jobs have all their start dates in the past.

My thought was to select the highest values of the start dates, grouped by jid and if they were in the past, it would imply that all other dates associated with the same job are also in the past.

This query isn't working because it's checking for start dates in the past and then selecting the highest of those. This doesn't exclude the possibility of another date for the same job, lying in the future.

I have no idea how I could proceed now. Any help or clue is appreciated.

Cheers - Tom


You have to use HAVING :

SELECT jid, MAX(start) as start
FROM dates
GROUP BY jid
HAVING MAX(start) < NOW();

HAVING acts a bit like WHERE. It filters out the rows after they were selected. Usually (and actually, I can't think of any other case), you only use HAVING with aggregate functions.

(I hope you really inserted dates in the future in your table, though!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜