Skip to content
Advertisement

How can I update date of datetime field with mysql only?

I have a table like this

| ID |         DATE              | DURATION |     STATUS | DUPLICATION |
|----|------------------- -------|----------|------------|-------------|
|  1 | January, 08 2014 19:30:12 | 00:00:03 |    Aborted |      (null) |
|  2 | January, 08 2014 19:30:12 | 00:00:06 |    Dropped |      (null) |
|  3 | January, 08 2014 05:25:11 | 00:00:20 |  Connected |    Multiple |
|  4 | January, 08 2014 05:19:21 | 00:00:21 |  Connected |      Repeat |
|  5 | January, 08 2014 05:12:56 | 00:00:20 |  Connected |      Unique |
|  6 | January, 08 2014 04:46:56 | 00:00:41 | Unanswered |    Multiple |
|  7 | January, 08 2014 04:42:56 | 00:00:35 | Unanswered |      Repeat |
|  8 | January, 08 2014 04:34:56 | 00:00:31 | Unanswered |      Unique |
|  9 | January, 09 2014 12:06:32 | 00:00:06 |    Dropped |      (null) |
| 10 | January, 09 2014 12:06:32 | 00:00:03 |    Aborted |      (null) |
| 11 | January, 09 2014 05:25:11 | 00:00:20 |  Connected |    Multiple |
| 12 | January, 09 2014 05:19:21 | 00:00:21 |  Connected |      Repeat |
| 13 | January, 09 2014 05:12:56 | 00:00:20 |  Connected |      Unique |
| 14 | January, 09 2014 04:46:56 | 00:00:41 | Unanswered |    Multiple |
| 15 | January, 09 2014 04:42:56 | 00:00:35 | Unanswered |      Repeat |
| 16 | January, 09 2014 04:34:56 | 00:00:31 | Unanswered |      Unique |
| 17 | January, 09 2014 12:19:01 | 00:00:20 |    Aborted |      (null) |
| 18 | January, 09 2014 12:19:01 | 00:00:19 |  Connected |      Repeat |
| 19 | January, 09 2014 12:15:30 | 00:00:20 |    Aborted |      (null) |
| 20 | January, 09 2014 12:15:30 | 00:00:19 |  Connected |      Unique |
| 21 | January, 09 2014 07:25:11 | 00:00:41 |  Connected |    Multiple |
| 22 | January, 09 2014 07:19:21 | 00:00:27 |  Connected |      Repeat |
| 23 | January, 09 2014 07:12:56 | 00:00:20 |  Connected |      Unique |
| 24 | January, 09 2014 06:46:56 | 00:00:32 | Unanswered |      Unique |
| 25 | January, 09 2014 06:42:56 | 00:00:29 |    Aborted |      (null) |
| 26 | January, 09 2014 06:34:56 | 00:00:27 |    Aborted |      (null) |
| 27 | January, 09 2014 05:34:56 | 00:00:27 |    Aborted |      (null) |
| 28 | January, 09 2014 05:25:11 | 00:00:41 |  Connected |    Multiple |
| 29 | January, 09 2014 05:19:21 | 00:00:27 |  Connected |      Repeat |
| 30 | January, 09 2014 05:12:56 | 00:00:20 |  Connected |      Unique |

I want to update only the date but dont know where to start from. I mean in the datetime field date i want to update the date ‘2014-01-08’ to ‘2014-01-01’. How can i do that? I have tried this.

UPDATE calldate cd
INNER JOIN calldate cdl ON cdl.id = cd.id
SET cd.date = ''/*Dont know what to do here */
WHERE DATE(cd.date) = '2014-01-08'

Here is the

Fiddle Structure

Advertisement

Answer

One option is to use this:

UPDATE `calldata`
SET `date` = DATE_SUB(`date`, INTERVAL 7 DAY)
WHERE DATE(`date`) = '2014-01-08'

Fiddle: http://sqlfiddle.com/#!2/28d71/1

For better performance use the following query because index (if any) can be used by MySQL as DATE() is not on the left side of comparison operator:

UPDATE `calldata`
SET `date` = DATE_SUB(`date`, INTERVAL 7 DAY)
WHERE `date` >= '2014-01-08'
AND `date` < '2014-01-09

Fiddle: http://sqlfiddle.com/#!2/7e7b2/1

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement