php mysql 如何筛选出每天的最后一条数据呢?
php程序里有这样一句:$logs_result=$database->query("SELECT*FROM`weibo_logs`WHERE`date`BETWEEN'...
php 程序里有这样一句:
$logs_result = $database->query("SELECT * FROM `weibo_logs` WHERE `date` BETWEEN '{$date_range_start}' AND DATE_ADD('{$date_range_end}', INTERVAL 1 DAY) AND `weibo_user_id` = '{$source_account->id}' ORDER BY `date` ASC");
我想在不改变原功能的前提下,加一个筛选,选出每天的最后一条数据,以上的代码需要如何修改呢?请指教,感谢! 展开
$logs_result = $database->query("SELECT * FROM `weibo_logs` WHERE `date` BETWEEN '{$date_range_start}' AND DATE_ADD('{$date_range_end}', INTERVAL 1 DAY) AND `weibo_user_id` = '{$source_account->id}' ORDER BY `date` ASC");
我想在不改变原功能的前提下,加一个筛选,选出每天的最后一条数据,以上的代码需要如何修改呢?请指教,感谢! 展开
2个回答
2019-10-18
展开全部
查询今天最后一条数据
SELECT * FROM `weibo_logs` where DATEDIFF( NOW(),date)=0 order by `date` desc limit 1
查询符合条件的weibo_user_id每天的一条数据
SELECT weibo_user_id, DATE_FORMAT(date,'%Y-%m-%d') FROM `text` where 'weibo_user_id'='{$source_account->id}' GROUP BY DATE_FORMAT(date,'%Y-%m-%d')
这样会有排序问题,那么就先排序
SELECT DATE_FORMAT(date,'%Y-%m-%d') as d
FROM (SELECT * FROM `text` ORDER BY date DESC) as s
where 'weibo_user_id'='{$source_account->id}'
GROUP BY DATE_FORMAT(date,'%Y-%m-%d')
SELECT * FROM `weibo_logs` where DATEDIFF( NOW(),date)=0 order by `date` desc limit 1
查询符合条件的weibo_user_id每天的一条数据
SELECT weibo_user_id, DATE_FORMAT(date,'%Y-%m-%d') FROM `text` where 'weibo_user_id'='{$source_account->id}' GROUP BY DATE_FORMAT(date,'%Y-%m-%d')
这样会有排序问题,那么就先排序
SELECT DATE_FORMAT(date,'%Y-%m-%d') as d
FROM (SELECT * FROM `text` ORDER BY date DESC) as s
where 'weibo_user_id'='{$source_account->id}'
GROUP BY DATE_FORMAT(date,'%Y-%m-%d')
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询