简体   繁体   中英

How can I increase a timestamp value by one for each result with same value in another column

I'm using a custom query to get purchased orders out of Magento, something like this:

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

The SQL works, but I need to use the data to do an import into a third party system and they do not accept same timestamp when the order ID is the same - the recommended solution is to do +1 on the timestamp for each row that has the same order id.

Is this even possible using pure SQL? I would need to keep track of the order # to see if it's been outputted before and if so do +1 for each time on the timestamp value.

So, say that I get a first result with order #123 and timestamp 45672 as example data, then for the next result I get same order #123 and need to do 45672+1 for timestamp = 45673, next row with same order # should be 45674 and so on, until the order id is unique again and i can use the actual value ".o.created_at"

Any suggestion, even if it requires PHP manipulation of the result?

It is possible with a couple queries and hacky temporary tables, but I don't recommend using this method for obvious reasons.

You can do it easily with PHP manipulation though. Just make sure the results are ordered by timestamp to make things easier. Example:

$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

}

This should make sure $row['timestamp'] is always different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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