简体   繁体   中英

how to get data by date from sql database

I have this code. In this code getting data by views.

But, I want to get data by date in create recent.

Like this code

$recenttuners = $db->rawQuery('SELECT user_id, date AS count 
                                FROM '.T_TUNES.' 
                                WHERE user_id NOT IN ('.implode(",", $pt->blocked_array).') 
                                GROUP BY user_id 
                                ORDER BY count DESC 
                                LIMIT 20');

code

$db->where('active_expire', time(),'<=')->update(T_USERS,array('active_expire' => $week_end,'active_time' => 0));
$recenttuners = $db->rawQuery('SELECT user_id, SUM(views) AS count FROM '.T_TUNES.' WHERE user_id NOT IN ('.implode(",", $pt->blocked_array).') GROUP BY user_id ORDER BY count DESC LIMIT 20');

foreach ($recenttuners as $key => $value) {
    $views_count = number_format($value->count);
    $views_ = $value->count;
    $followers = $db->rawQuery('SELECT COUNT(*) AS count FROM '.T_FOLLOWING_TUNES.' WHERE user_id = '.$value->user_id.' GROUP BY user_id LIMIT 1');
    $followers_count = 0;
    if (isset($followers[0])) {
        $followers_count = ($followers[0]->count > 0) ? number_format($followers[0]->count) : 0;
    }
    $user = PT_UserData($value->user_id);
}

Instead of ORDER BY count DESC LIMIT 20 you should do something like: ORDER BY date DESC LIMIT 20 depending on how your date field is called in database.

$recenttuners = $db->rawQuery('SELECT user_id, date AS count 
                                FROM '.T_TUNES.' 
                                WHERE user_id NOT IN ('.implode(",", $pt->blocked_array).') 
                                GROUP BY user_id 
                                ORDER BY date DESC 
                                LIMIT 20');

Change the date to the parameter that is in database. It could be named created_at createdAt etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM