【问题标题】:How can I increase a timestamp value by one for each result with same value in another column如何为另一列中具有相同值的每个结果将时间戳值增加一个
【发布时间】:2014-08-15 07:02:00
【问题描述】:

我正在使用自定义查询从 Magento 获取购买的订单,如下所示:

SELECT customer_id, UNIX_TIMESTAMP(o.created_at) as timestamp, increment_id as order_id .... joins here...

SQL 有效,但我需要使用数据导入第三方系统,并且当订单 ID 相同时,它们不接受相同的时间戳 - 推荐的解决方案是在时间戳上 +1具有相同订单 ID 的每一行。

这甚至可以使用纯 SQL 实现吗?我需要跟踪订单 # 以查看它之前是否已输出,如果是,则每次在时间戳值上 +1。

所以,假设我得到第一个结果,订单 #123 和时间戳 45672 作为示例数据,然后对于下一个结果,我得到相同的订单 #123 并且需要为时间戳 = 45673 执行 45672+1,下一行具有相同订单号应该是 45674 以此类推,直到订单 ID 再次唯一并且我可以使用实际值“.o.created_at”

任何建议,即使它需要对结果进行 PHP 操作?

【问题讨论】:

    标签: php mysql sql magento timestamp


    【解决方案1】:

    可能有几个查询和 hacky 临时表,但我不建议使用这种方法,原因很明显。

    不过,您可以通过 PHP 操作轻松做到这一点。只需确保结果按timestamp 排序以使事情变得更容易。示例:

    $last_timestamp = 0;
    while ($row = mysql_fetch_assoc($result))
    {
        if ($row['timestamp'] <= $last_timestamp)
            $row['timestamp'] = $last_timestamp + 1;
        $last_timestamp = $row['timestamp'];
    
        // ... do stuff
    
    }
    

    这应该确保$row['timestamp'] 总是不同的。

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多