Home > Enterprise >  How to add timestamp with interval starting from Current time with interval 1 minutes for every row
How to add timestamp with interval starting from Current time with interval 1 minutes for every row

Time:10-24

In example, I have something like,

Number_index
1
2
3
4
etc

for example, using NOW() will give time stamp "10/24/2022 12:00:00" I want to add timestamp which will be something like

Number_index Time_Stamp
1 10/24/2022 12:00:00
2 10/24/2022 12:01:00
3 10/24/2022 12:02:00
4 10/24/2022 12:03:00
etc etc

Would you please explain how to do that in mysql? Thank you

CodePudding user response:

We can use the TIMESTAMPADD() function here:

SELECT Number_index,
       TIMESTAMPADD(MINUTE, Number_index - 1, NOW()) AS Time_Stamp
FROM yourTable
ORDER BY Number_index;

The above is one option if you want view your output as described. If instead you want to update your table, then use:

UPDATE yourTable
SET Time_Stamp = TIMESTAMPADD(MINUTE, Number_index - 1, NOW());
  • Related