从select中创建临时表,使用日期时间导致无效的默认值错误

时间:2016-12-08 12:56:05

标签: mysql default-value

我有一个带有字段ts的表,它是一个日期时间和默认值current_timestamp。

如果我尝试通过

从此表创建临时表
CREATE TEMPORARY TABLE tblTmp (
    tmpid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    INDEX(tmpid)
) AS
    SELECT * FROM myTable;

由于列ts的默认值无效,我收到错误。

我知道这与mysql 5.7中的严格模式有关。

有没有办法解决这个问题,因为我完全不明白为什么curr_timestamp作为默认值会出现此错误。

更新:刚发现问题是使用NOW()或CURRENT_TIMESTAMP与select。我删除该行,它可以工作。

所以:

SELECT col1, col2, ... FROM myTable 

正在运作

SELECT col1, col2, NOW() as currDatetime FROM myTable

无效。

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

如果您选择了所需的字段,则可以在选择中规避问题或代码。 例如给出

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `userName` varchar(60) NOT NULL,
  `photo` varchar(50) NOT NULL,
  `status` int(11) NOT NULL,
  `ts` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1

MariaDB [sandbox]> select * from users;
+-----+----------+--------------+--------+---------------------+
| id  | userName | photo        | status | ts                  |
+-----+----------+--------------+--------+---------------------+
|   1 | John     | john.png     |      1 | 2016-12-08 13:14:24 |
|   2 | Jane     | jane.png     |      1 | 2016-12-08 13:14:24 |
|   3 | Ali      |              |      1 | 2016-12-08 13:14:24 |
|   6 | Bruce    | bruce.png    |      1 | 2016-12-08 13:14:24 |
|   7 | Martha   |              |      1 | 2016-12-08 13:14:24 |
|   8 | Sidney   |              |      1 | 2016-12-08 13:14:24 |
|  10 | Charlie  | charlie.png  |      1 | 2016-12-08 13:14:24 |
|  12 | Elisa    |              |      1 | 2016-12-08 13:14:24 |
|  14 | Samantha | samantha.png |      1 | 2016-12-08 13:14:24 |
|  15 | Hannah   | hannah.png   |      1 | 2016-12-08 13:14:24 |
|  16 | Hannah   |              |      1 | 2016-12-08 13:14:24 |
|  17 | Kevin    | kevin1.png   |      1 | 2016-12-08 13:14:24 |
|  18 | Kevin    | kevin2.png   |      1 | 2016-12-08 13:14:24 |
|  19 | Ruth     |              |      1 | 2016-12-08 13:14:24 |
| 999 | xxx      | photo        |      1 | 2016-12-08 13:16:41 |
+-----+----------+--------------+--------+---------------------+
15 rows in set (0.00 sec)

MariaDB [sandbox]> drop  table if exists tbltmp;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> CREATE TEMPORARY TABLE tblTmp (
    ->     tmpid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->     INDEX(tmpid)
    -> ) AS
    ->     SELECT id,ts FROM users;
Query OK, 15 rows affected (0.19 sec)
Records: 15  Duplicates: 0  Warnings: 0

结果

MariaDB [sandbox]>
MariaDB [sandbox]> select * from tbltmp;
+-------+-----+---------------------+
| tmpid | id  | ts                  |
+-------+-----+---------------------+
|     1 |   1 | 2016-12-08 13:14:24 |
|     2 |   2 | 2016-12-08 13:14:24 |
|     3 |   3 | 2016-12-08 13:14:24 |
|     4 |   6 | 2016-12-08 13:14:24 |
|     5 |   7 | 2016-12-08 13:14:24 |
|     6 |   8 | 2016-12-08 13:14:24 |
|     7 |  10 | 2016-12-08 13:14:24 |
|     8 |  12 | 2016-12-08 13:14:24 |
|     9 |  14 | 2016-12-08 13:14:24 |
|    10 |  15 | 2016-12-08 13:14:24 |
|    11 |  16 | 2016-12-08 13:14:24 |
|    12 |  17 | 2016-12-08 13:14:24 |
|    13 |  18 | 2016-12-08 13:14:24 |
|    14 |  19 | 2016-12-08 13:14:24 |
|    15 | 999 | 2016-12-08 13:16:41 |
+-------+-----+---------------------+
15 rows in set (0.00 sec)

您可以在select中使用别名为来自源表的列指定新名称。