如何仅使用mysql更新datetime字段的日期

时间:2014-01-09 09:48:43

标签: mysql sql date datetime sql-update

我有一张这样的表

| 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 |

我想只更新日期,但不知道从哪里开始。我的意思是在日期时间字段date中我想将日期'2014-01-08'更新为'2014-01-01'。我怎样才能做到这一点? 我试过这个。

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'

以下是

Fiddle Structure

4 个答案:

答案 0 :(得分:14)

一种选择是使用它:

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

小提琴:http://sqlfiddle.com/#!2/28d71/1

为了获得更好的性能,请使用以下查询,因为MySQL可以使用索引(如果有),因为DATE()不在比较运算符的左侧:

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

小提琴:http://sqlfiddle.com/#!2/7e7b2/1

答案 1 :(得分:8)

试试这个:

UPDATE calldata
SET date = CONCAT('2014-01-01 ', TIME(date))
WHERE DATE(date) = '2014-01-08'

检查SQL FIDDLE DEMO

<强>输出

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

答案 2 :(得分:3)

UPDATE calldata
SET date = '2014-01-01'
WHERE DATE(date) = '2014-01-08'

SQLFiddle demo

答案 3 :(得分:0)

template<template <class, class> class C, class T, class A>
void DoSomethingSmartly(
    typename std::enable_if<
        supports_iteration<
            C<T,A>
        >::value
    >::type& val)
{
    T* pT;
    cout << "did something smartly!\n";
}

UPDATE calldata SET date = '2014-01-01' WHERE DATE(date) = '2014-01-01' 

两个案例没有任何问题,在我的情况下,日期类型为datetime。