2017-02-28 125 views
0

試圖建立一個系統,檢查巫婆用戶在過去的14,60或120天內沒有登錄過。如果用戶未在14天內登錄但是同時向該用戶發送電子郵件60天和120天,我想發送電子郵件。Mysql:檢查用戶是否在過去的14,60或120天內沒有登錄

:user_counts user_count當天有超過80 000行。

:時間日期時間在MySQL

// Number of days a user have not been login 
$dateCheck = array('14', '60', '120'); 

foreach ($dateCheck as $day) {   

    $userList = $wpdb->get_results(' 
    SELECT id, user_id 
    FROM user_counts 

    WHERE DATE(time) = "'.date("Y-m-d", strtotime("-".$day." day")).'" 
     AND time != (
      SELECT MAX(time) FROM user_counts 
      WHERE time < DATE_SUB(NOW(), INTERVAL '.$day.' DAY) 
    ) 
    GROUP BY user_id 
    '); 

} 

結果 我得到了所有的14,60及120天同一USER_ID。

+0

是時間字段時間戳嗎? – Naincy

+0

這個查詢現在返回什麼? – Akintunde007

+0

在反饋之後更新 – user2673664

回答

0
// Number of days a user have not been login 
$dateCheck = array('14', '60', '160'); 

$res = $wpdb->get_results(' 
    SELECT id, user_id, MAX(DATE(time)) as t 
    FROM user_counts  
    GROUP BY user_id 
    HAVING t <= "'.date("Y-m-d", strtotime("-".min($dateCheck)." day")).'" 
    ORDER BY t 
'); 

rsort($dateCheck); 
foreach ($res as $result) { 
    $date = date_create($res); 

    foreach ($dateCheck as $day) { 

     if ($result->t == date("Y-m-d", strtotime("-".$day." day"))) { 

      // Send for exempel a mail 
      // check witch email temple to use by using: $day 

     } 
    } 
} 
+0

是不是更快在Mysql中直接提出這個請求呢? – user2673664

+0

@ user2673664您是否需要將這些結果傳遞給PHP?如果你在談論日期,那麼是的,這可能會更好。 – apokryfos

+0

是的,我會發郵件給這個用戶說「很久..」 – user2673664