繁体   English   中英

比较多维数组中的两个DATETIME字段并返回值或索引

[英]Compare two DATETIME fields from multidimensional array and return a value or index

我有一个用户表。 我要查看的每行有2个DATETIME 我将查看$lastLog日期和$lastReceived (用于最后收到的潜在客户),并确定哪个较新,并成为其$eligibleTime 然后,我们将寻找最早的$eligibleTime以选择应该获得潜在客户的用户。 我正在筛选用户,以确保他们在潜在客户进入的适当状态下获得许可,并且还没有达到每日配额。

我在想也许我建立了错误的multi-d数组,应该将所有$lastLog日期和$lastReceived日期分组到自己的数组中,然后运行max($array)或类似的东西。

我怎么看这个? 我使用以下方法构建了多维数组:

    $sql = "SELECT `lastLog`,`firstName`,`lastReceived` FROM customers";
    $sql = mysqli_query($conn,$sql);
    $result = array();
    while($line = mysqli_fetch_assoc($sql)){
      $result[] = $line;
    }

它返回以下数据:

> Array ( [0] => Array ( [lastLog] => 2015-06-12 02:00:00 [firstName] => Nathaniel [lastReceived] => 2015-06-12 05:16:10 ) [1] => Array ( [lastLog] => 2015-06-12 01:00:00 [firstName] => Ignacio [lastReceived] => 2015-06-01 10:00:00 ) [2] => Array ( [lastLog] => 2015-06-12 00:00:00 [firstName] => James [lastReceived] => 2015-06-08 00:00:00 ) [3] => Array ( [lastLog] => 2015-06-12 04:00:00 [firstName] => Robert [lastReceived] => 2015-06-10 00:00:00 ) ) 

谢谢,纳特

多亏了Marc B,这才得以解决。

$sql = mysqli_query($conn,
   "SELECT firstName, greatest(lastLog, lastReceived) AS eligibleTime 
    FROM customers 
    ORDER BY eligibleTime DESC
    LIMIT 1");
$result = mysqli_fetch_assoc($sql);
print_r($result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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