我有一张桌子上有带有时间戳的记录。现在我有了一个要求,那就是获得最古老的唱片和最新的唱片。
9/10/2014 6:54
9/10/2014 6:53
9/10/2014 6:51
9/8/2014 8:09
9/8/2014 7:00
9/5/2014 7:38
9/5/2014 3:57
9/5/2014 3:51
9/4/2014 11:09
9/4/2014 8:39目前,我通过发送两个数据库调用来获得它们,这减慢了处理速度。
$new_timestamp = mysql_query("SELECT TIMESTAMP FROM $rectable ORDER BY TIMESTAMP DESC LIMIT 1");
$col_old = mysql_fetch_assoc($new_timestamp);
$old = $col_new['TIMESTAMP'];
$new_timestamp1 = mysql_query("SELECT TIMESTAMP FROM $rectable ORDER BY TIMESTAMP ASC LIMIT 1");
$col_new = mysql_fetch_assoc($new_timestamp1);
$new = $col_new['TIMESTAMP'];有没有办法在不发送两个数据库调用的情况下,通过一个特殊的查询或存储过程来优化此代码和完整的需求?
发布于 2014-12-05 04:24:24
您可以使用max和min获得最新和最古老的时间戳。
select max(timestamp), min(timestamp) from mytable发布于 2014-12-05 04:23:02
尝试使用UNION
select TIMESTAMP as old FROM $rectracktable ORDER BY TIMESTAMP DESC LIMIT 1
union all
select TIMESTAMP as new FROM $rectable ORDER BY TIMESTAMP ASC LIMIT 1https://stackoverflow.com/questions/27308693
复制相似问题