Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate DATE from YEAR, WEEK and WEEKDAY in MySQL

Tags:

date

sql

mysql

Pretty much what the title says

I basically want to do a simple query...

SELECT *
FROM sign_in_out
WHERE user_id = 6
  AND time_signed < DATE('2014-10-23 16:11:54')
ORDER BY time_signed DESC LIMIT 1

...to get the nearest signing in/out activity prior to the given day, to determine whether the user in question was signed in or out when the day started. The query above works fine but the problem is I don't have the date like that, I have the YEAR, the WEEK, and the WEEKDAY.

How do I create a DATE from these in MySQL?

Thanks


1 Answers

SELECT *
FROM sign_in_out
WHERE user_id = 6
  AND time_signed < MAKEDATE(year_col,(week_col*7+(weekday-1)))
ORDER BY time_signed DESC LIMIT 1

Check Manual MAKEDATE

like image 164
Sadikhasan Avatar answered Nov 28 '25 17:11

Sadikhasan