如何在 mariadb 或 mysql 中创建临时表?

How can I create a temporary table in mariadb or mysql?(如何在 mariadb 或 mysql 中创建临时表?)

本文介绍了如何在 mariadb 或 mysql 中创建临时表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在将 mssql 迁移到 mariadb(mysql).mssql有with子句(with A as ()),但是mysql没有with子句.所以我使用创建临时表",但出现了一些错误.

Now, I'm migrating mssql to mariadb(mysql). Mssql has with clause(with A as ()), but mysql has not with clause. So I'm using "create temporary table", but some error occurred.

例子,

CREATE TEMPORARY TABLE TMP(
    SELECT CAST(DATE_FORMAT(NOW(), '%Y-%m-%d %T.%f') AS CHAR(19)) AS REGDT
);

这是错误代码

MySQL 数据库错误:截断不正确的 CHAR(19) 值:'2016-06-27 10:34:34.000000'

MySQL Database Error: Truncated incorrect CHAR(19) value: '2016-06-27 10:34:34.000000'

怎么了?

这是我的查询.

CREATE TEMPORARY TABLE TMP(
  SELECT DEPTCODE, DEPTNAME, USEYN, HIGHDEPTCODE, 
         CAST(DATE_FORMAT(REGDT, '%Y-%m-%d %T.%f') AS CHAR(19)) AS REGDT,
         0 AS LEV, CAST(DEPTCODE AS CHAR(255)) AS SORT, DEPTORDER 
    FROM TBDEPARTMENT
   WHERE DEPTCODE = '1000');

推荐答案

CHAR(19) 太小,无法以毫秒为单位存储整个日期.

CHAR(19) is too small to store your whole date with milliseconds.

改用 CHAR(26):

Use CHAR(26) instead:

CREATE TEMPORARY TABLE TMP(
    SELECT CAST(DATE_FORMAT(NOW(), '%Y-%m-%d %T.%f') AS CHAR(26)) AS REGDT
);

或删除毫秒:

CREATE TEMPORARY TABLE TMP(
    SELECT CAST(DATE_FORMAT(NOW(), '%Y-%m-%d %T') AS CHAR(19)) AS REGDT
);

您也可以使用 DATETIME 类型而不是字符串.

You could also use a DATETIME type instead of a String.

这篇关于如何在 mariadb 或 mysql 中创建临时表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 mariadb 或 mysql 中创建临时表?

基础教程推荐