-2

我有一个像这样的查询:

 select * from posts p where p.post like '%cadillac%' ORDER BY p.upvotes DESC,
 p.unix_timestamp DESC LIMIT 20

现在,在遍历数组时,我需要找到最小值unix_timestamp。就像是:

 $counter = 0;
 while($row = $result->fetch_assoc()){

 // what do i do here to find the minimum timestamp value 

 $counter = $counter + 1;
 }

请指教。

4

3 回答 3

2
 $counter = 0;
 $min_timestamp = 0;
 while($row = $result->fetch_assoc()){

     //check if min hasn't been set OR if the current timestamp is less than the min
     if(!$min_timestamp || ($row['unix_timestamp'] < $min_timestamp)) {
         //store this timestamp as the current min
         $min_timestamp = $row['unix_timestamp'];
     } 

     //$counter = $counter + 1;
 }
 //once we reach here, min_timestamp will hold the minimum timestamp found
于 2013-03-25T17:37:07.580 回答
1

以下将起作用:

$counter = 0;
$min_value;
while($row = $result->fetch_assoc()){
  $min_value = is_null($min_value) || $min_value > $row['unix_timestamp'] ? $row['unix_timestamp'] :$min_value  ;

  $counter = $counter + 1;
}
于 2013-03-25T17:38:48.070 回答
0

您可以在 SQL 中执行此操作

select MIN(unix_timestamp) from posts p where p.post like '%cadillac%'
于 2013-03-25T17:36:14.207 回答