如何制作带有日期和时间列的mysql表? [英] How to make a mysql table with date and time columns?

查看:131
本文介绍了如何制作带有日期和时间列的mysql表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个表,该表的日期和时间列用于在数据库中记录该条目时用于存储日期和时间信息.在MySQL文档中显示了以下示例:

I am trying to create a table that has date and time columns used to store the date and time information when the entry is recorded in the database. In the MySQL documentation shows the following example:

CREATE TABLE t1 (
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  dt DATETIME DEFAULT CURRENT_TIMESTAMP
);

问题是我想要日期和时间的单独字段.在另一个站点上,我找到了适合我日期要求的这段代码:

The problem is that I want separate fields for date and time. On another site I found this piece of code that suits my needs for the date:

CREATE TABLE test (id INT, date_added DATE);

但是我当时找不到类似的东西. 以下代码段是否将记录记录条目时填写的单独的日期和时间字段?

but I wasn't able to find anything like that for the time. Will the following piece of code make separate date and time fields that would be filled as the entry is recorded?

CREATE TABLE test (id INT, date_added DATE, time_added TIME);

推荐答案

在日期和时间的单独列中没有意义.没什么意义

There is no point in having separate columns for Date and Time. It does not make much sense

您可以这样创建表

CREATE TABLE timeDate (
   id INT,
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
);

如果要日期"部分使用此查询

And if you want the Date part use this query

SELECT DATE(`ts`) FROM timedate WHERE id =someId

如果您希望时间部分使用此查询

And if you want the Time part use this query

SELECT TIME(`ts`) FROM timedate WHERE id =someId

这篇关于如何制作带有日期和时间列的mysql表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆