简体   繁体   English

如何仅从 TIMESTAMP(6) mysql 恢复日期时间和时间?

[英]how recovery ONLY datetime and TIME from TIMESTAMP(6) mysql?

I have this database:我有这个数据库:

    CREATE TABLE `pruebas` (
    `id` mediumint(8) UNSIGNED NOT NULL,
    `datetime` datetime(6) NOT NULL,
    `timestamp` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    `time` time(6) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

    INSERT INTO `pruebas` (`id`, `datetime`, `timestamp`, `time`) VALUES
    (1, '2020-10-27 15:27:30.039951', '2020-10-27 20:27:30.039951', '15:27:30.039951'),
    (2, '2020-10-27 15:27:31.162827', '2020-10-27 20:27:31.162827', '15:27:31.162827'),
    (3, '2020-10-27 15:28:20.158641', '2020-10-27 20:28:20.158641', '15:28:20.158641'),
    (4, '2020-10-27 15:32:17.708642', '2020-10-27 20:32:17.708642', '15:32:17.708642'),
    (5, '2020-10-27 15:32:25.938338', '2020-10-27 20:32:25.938338', '15:32:25.938338'),
    (6, '2020-10-27 15:32:42.150837', '2020-10-27 20:32:42.150837', '15:32:42.150837'),
    (7, '2020-10-27 15:32:50.849249', '2020-10-27 20:32:50.849249', '15:32:50.849249'),
    (8, '2020-10-27 15:34:57.296413', '2020-10-27 20:34:57.296413', '15:34:57.296413'),
    (9, '2020-10-27 15:35:21.746913', '2020-10-27 20:35:21.746913', '15:35:21.746913'),
    (10, '2020-10-27 15:35:36.356490', '2020-10-27 20:35:36.356490', '15:35:36.356490'),
    (11, '2020-10-27 15:37:50.815179', '2020-10-27 20:37:50.815179', '15:37:50.815179'),
    (12, '2020-10-27 15:39:13.878842', '2020-10-27 20:39:13.878842', '15:39:13.878842'),
    (13, '2020-10-27 15:39:24.146655', '2020-10-27 20:39:24.146655', '15:39:24.146655'),
    (14, '2020-10-27 15:39:51.128294', '2020-10-27 20:39:51.128294', '15:39:51.128294');

    ALTER TABLE `pruebas`
    ADD UNIQUE KEY `id` (`id`);

    ALTER TABLE `pruebas`
    MODIFY `id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=22;
    COMMIT;

how I can recovery ONLY DATETIME from any of this columns?如何从任何此列中恢复 ONLY DATETIME?

I try with DATE_FORMAT, FROM_UNIXTIME, UNIX_TIMESTAMP, TRUNCATE, SEC_TO_TIME, and others "tricks/functions", but never I get: datetime :我尝试使用 DATE_FORMAT、FROM_UNIXTIME、UNIX_TIMESTAMP、TRUNCATE、SEC_TO_TIME 和其他“技巧/功能”,但我从来没有得到: datetime

by example from ID 1 I need the result datetime = "2020-10-27 15:27:30", time = 15:27:30以 ID 1 为例,我需要结果 datetime = "2020-10-27 15:27:30", time = 15:27:30

Thanks谢谢

You can cast() :你可以cast()

select cast('2020-10-27 15:27:30.039951' as datetime) dt

For your sample data, this query:对于您的示例数据,此查询:

select p.*, 
    cast(`datetime` as datetime) datetime1,
    cast(`timestamp` as datetime) timestamp1,
    cast(`time` as datetime) time1
from pruebas p

Produces:产生:

id | datetime                   | timestamp                  | time            | datetime1           | timestamp1          | time1              
-: | :------------------------- | :------------------------- | :-------------- | :------------------ | :------------------ | :------------------
 1 | 2020-10-27 15:27:30.039951 | 2020-10-27 20:27:30.039951 | 15:27:30.039951 | 2020-10-27 15:27:30 | 2020-10-27 20:27:30 | 2020-10-27 15:27:30
 2 | 2020-10-27 15:27:31.162827 | 2020-10-27 20:27:31.162827 | 15:27:31.162827 | 2020-10-27 15:27:31 | 2020-10-27 20:27:31 | 2020-10-27 15:27:31
 3 | 2020-10-27 15:28:20.158641 | 2020-10-27 20:28:20.158641 | 15:28:20.158641 | 2020-10-27 15:28:20 | 2020-10-27 20:28:20 | 2020-10-27 15:28:20
 4 | 2020-10-27 15:32:17.708642 | 2020-10-27 20:32:17.708642 | 15:32:17.708642 | 2020-10-27 15:32:18 | 2020-10-27 20:32:18 | 2020-10-27 15:32:18
 5 | 2020-10-27 15:32:25.938338 | 2020-10-27 20:32:25.938338 | 15:32:25.938338 | 2020-10-27 15:32:26 | 2020-10-27 20:32:26 | 2020-10-27 15:32:26
 6 | 2020-10-27 15:32:42.150837 | 2020-10-27 20:32:42.150837 | 15:32:42.150837 | 2020-10-27 15:32:42 | 2020-10-27 20:32:42 | 2020-10-27 15:32:42
 7 | 2020-10-27 15:32:50.849249 | 2020-10-27 20:32:50.849249 | 15:32:50.849249 | 2020-10-27 15:32:51 | 2020-10-27 20:32:51 | 2020-10-27 15:32:51
 8 | 2020-10-27 15:34:57.296413 | 2020-10-27 20:34:57.296413 | 15:34:57.296413 | 2020-10-27 15:34:57 | 2020-10-27 20:34:57 | 2020-10-27 15:34:57
 9 | 2020-10-27 15:35:21.746913 | 2020-10-27 20:35:21.746913 | 15:35:21.746913 | 2020-10-27 15:35:22 | 2020-10-27 20:35:22 | 2020-10-27 15:35:22
10 | 2020-10-27 15:35:36.356490 | 2020-10-27 20:35:36.356490 | 15:35:36.356490 | 2020-10-27 15:35:36 | 2020-10-27 20:35:36 | 2020-10-27 15:35:36
11 | 2020-10-27 15:37:50.815179 | 2020-10-27 20:37:50.815179 | 15:37:50.815179 | 2020-10-27 15:37:51 | 2020-10-27 20:37:51 | 2020-10-27 15:37:51
12 | 2020-10-27 15:39:13.878842 | 2020-10-27 20:39:13.878842 | 15:39:13.878842 | 2020-10-27 15:39:14 | 2020-10-27 20:39:14 | 2020-10-27 15:39:14
13 | 2020-10-27 15:39:24.146655 | 2020-10-27 20:39:24.146655 | 15:39:24.146655 | 2020-10-27 15:39:24 | 2020-10-27 20:39:24 | 2020-10-27 15:39:24
14 | 2020-10-27 15:39:51.128294 | 2020-10-27 20:39:51.128294 | 15:39:51.128294 | 2020-10-27 15:39:51 | 2020-10-27 20:39:51 | 2020-10-27 15:39:51

Demo on DB Fiddle DB Fiddle 上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM