Frage

I am trying to update a time value in my MySQL table and am having trouble working the logic out.

I want to make it so it reads:

ID  Time
1   17:00
2   17:00
3   17:08
4   17:08
5   17:16
6   17:16

and so on.

I can select every second row by using MOD on the ID number but I am having trouble specifying the correct update value for the row.

UPDATE `table`
set `Time` = DATE_ADD(Convert('17:00:00',TIME),INTERVAL (`ID`-2)*8 MINUTE)
where `ID` mod 2 = 1

If you require any clarifications please let me know and thanks in advance.

UPDATE: I should add that if I there is a query that can update like the table above then that would be even better!

War es hilfreich?

Lösung

I assume this is what you want:

update tbl set `Time` =
           ADDDATE('1970-01-01 17:00:00', INTERVAL 8*((`ID`-1) DIV 2) MINUTE);

Producing (for a table already having rows ID1~6):

mysql> select * from tbl;
+------+----------+
| ID   | Time     |
+------+----------+
|    1 | 17:00:00 |
|    2 | 17:00:00 |
|    3 | 17:08:00 |
|    4 | 17:08:00 |
|    5 | 17:16:00 |
|    6 | 17:16:00 |
+------+----------+
6 rows in set (0.00 sec)

Andere Tipps

your mod is selecting impair numbers not pair numbers (every second)

try this

      where `ID` mod 2 = 0
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top