MySQL Select records for the last 2 days of a specific week / month

I have 2 columns, "create_time" is when the account is registered, and "last_play" is when the account was last registered. I want to select accounts that have been registered in a particular week / month and have been active for the last 2 days of that particular week / month.

This is how I select ALL records of the last week without considering last_play (works):

SELECT COUNT(id) FROM account.account 
WHERE WEEKOFYEAR(create_time) = WEEKOFYEAR(NOW()) - 1 
AND YEAR(create_time) = YEAR(NOW());

What is my current request last week that doesn't work:

SELECT COUNT(id) FROM account.account 
WHERE WEEKOFYEAR(create_time) = WEEKOFYEAR(NOW()) - 1 
AND YEAR(create_time) = YEAR(NOW()) 
AND DATE(last_play) BETWEEN 
    ADDDATE(DATE(DATE_SUB(NOW(), INTERVAL 1 WEEK)), 
    INTERVAL 1 - DAYOFWEEK(DATE(NOW())) DAY) 
    AND DATE(NOW());
+4
source share
1 answer

, MySQL WEEKDAY :

SELECT COUNT(id) FROM account.account 
WHERE WEEKOFYEAR(create_time) = WEEKOFYEAR(NOW()) - 1 
AND YEAR(create_time) = YEAR(NOW())
AND WEEKOFYEAR(last_play) = WEEKOFYEAR(create_time)  //last_play is in the same week as create_time
AND WEEKDAY(last_play) IN (5,6);  //wekkday is saturday or sunday

, , .

EDIT: , WEEKOFYEAR MONTH WEEKDAY DAYOFMONTH. , :

SELECT COUNT(id) FROM account.account 
WHERE MONTH(create_time) = MONTH(NOW()) - 1 
AND YEAR(create_time) = YEAR(NOW())
AND MONTH(last_play) = MONTH(create_time)  //last_play is in the same MONTH as create_time
AND 
   (DAYOFMONTH(last_play) IN (30,31) AND MONTH(last_play) IN (1,3,5,7,8,10,12)
    OR DAYOFMONTH(last_play) IN (29,30) AND MONTH(last_play) IN (4,6,9,11)
    OR DAYOFMONTH(last_play) IN (27,28) AND MONTH(last_play) IN (2))

, ;-). .

+1

Source: https://habr.com/ru/post/1545462/


All Articles