mysql - Get last "active" day from last week -
in tool users can set workdays (example: monday friyday).when save settings, stored in mysql user table.
in next week (this week) "last workday". in case friday. how can last workday every user mysql?
currently save workdays in csv (2,3,4,5,6 - 2 = monday...) can change that.
i tried stuff weekday(), doesnt work.
can me?
dayname(concat('1970-09-2', substring_index(workdays, ',', -1)))
explanation:
this mysql string function substring_index
:
substring_index(workdays, ',', -1)
...will give number of last workday listed. (what find positions there's comma , returns after last comma, e.g., substring_index('1,2,3', ',', -1)
returns 3.)
and trick here, using dayname
, concat
:
dayname(concat('1970-09-2', dayindex))
...gives day name. (since dayname
requires date, technique picks date in past sunday , ends in 0, in case 1970-09-20
, , replaces last digit returned index determine corresponding day of week, e.g., 20 = sunday, 21 = monday, etc.)
put them together:
select dayname(concat('1970-09-2', substring_index(workdays, ',', -1))) lastday ...
...and corresponding name of weekday returned in lastday
variable.
Comments
Post a Comment