How to make a mysql table with date and time columns?

前端 未结 2 1852
长发绾君心
长发绾君心 2021-01-15 04:52

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 documentati

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-15 05:36

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

    You can create the table like this

    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
    

提交回复
热议问题