dragomir
dragomir

Reputation: 61

How to compare the year to timestamp date

I want to take all the records from a specific year. My table is race_details and the year is a part of the date in race_date (in unix timestamp)

SELECT * 
FROM race_details AS r 
WHERE r.race_date=(SELECT MAX(YEAR(FROM_UNIXTIME(race_date))) 
                   FROM race_details)

Upvotes: 2

Views: 57

Answers (2)

rink.attendant.6
rink.attendant.6

Reputation: 46317

Unless there's more to the problem, I think you're overcomplicating your query:

SELECT * 
FROM race_details AS r 
WHERE YEAR(race_date) = 2014;

Substitute 2014 for the year to look up, and if it's a variable, remember to parameterize!

Upvotes: 3

Barmar
Barmar

Reputation: 782785

You need to compare the year of the race_date, not the whole race_date:

WHERE YEAR(race_date) = (SELECT ...)

Upvotes: 1

Related Questions