Programing

MySQL에서 현재 날짜와 시간을 얻는 방법?

lottogame 2020. 6. 23. 07:40
반응형

MySQL에서 현재 날짜와 시간을 얻는 방법?


현재 날짜와 시간을 삽입하기 위해 수동 쿼리에서 사용할 수있는 DATETIME과 같은 값이나 명령이 있습니까?

INSERT INTO servers (
  server_name, online_status, exchange, disk_space, network_shares
) VALUES(
  'm1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE' 'DATETIME' 
)

끝에 인용 된 DATETIME 값은 현재 날짜와 시간을 추가하려는 곳입니다.


당신은 사용할 수 있습니다 NOW():

INSERT INTO servers (server_name, online_status, exchange, disk_space, network_shares, c_time)
VALUES('m1', 'ONLINE', 'exchange', 'disk_space', 'network_shares', NOW())

CURRENT_TIMESTAMP () 또는 now ()를 사용하십시오.

처럼

INSERT INTO servers (server_name, online_status, exchange, disk_space,
network_shares,date_time) VALUES('m1','ONLINE','ONLINE','100GB','ONLINE',now() )

또는

INSERT INTO servers (server_name, online_status, exchange, disk_space,
network_shares,date_time) VALUES('m1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE'
,CURRENT_TIMESTAMP() )

date_time을 시간을 삽입하는 데 사용하려는 열 이름으로 바꾸십시오.


허용되는 답변이 많이 있지만이 방법도 가능하다고 생각합니다.

다음과 같이 '서버' 테이블을 작성하십시오 .

CREATE TABLE `servers`
(
      id int(11) NOT NULL PRIMARY KEY auto_increment,
      server_name varchar(45) NOT NULL,
      online_status varchar(45) NOT NULL,
      _exchange varchar(45) NOT NULL, 
      disk_space varchar(45) NOT NULL,
      network_shares varchar(45) NOT NULL,
      date_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);

그리고 당신의 INSERT의 문이 있어야합니다 :

INSERT INTO servers (server_name, online_status, _exchange, disk_space, network_shares)
VALUES('m1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE');

내 환경 :

4GB RAM이 장착 된 Core i3 Windows 랩톱에서 MySQL Workbench 6.2 (버전 6.2.5.0 빌드 397 64 비트)에서 위의 예를 수행했습니다.


예, CURRENT_TIMESTAMP()명령을 사용할 수 있습니다 .

여기를 참조하십시오 : 날짜 및 시간 함수


INSERT INTO servers (server_name, online_status, exchange, disk_space, network_shares)
VALUES('m1','ONLINE','exchange','disk_space','network_shares', NOW())

데이터베이스 디자인에서 일관성과 인덱싱 / 검색 / 비교 성능을 위해 Unixtime을 사용하는 것이 좋습니다.

UNIX_TIMESTAMP() 

나중에 사람이 읽을 수있는 형식으로 변환 할 수 있으므로 개별적으로 가장 편리합니다.

FROM_ UNIXTIME (unix_timestamp, [format ])

기본값을 변경하면 CURRENT_TIMESTAMP더 효율적입니다.

ALTER TABLE servers MODIFY COLUMN network_shares datetime NOT NULL DEFAULT CURRENT_TIMESTAMP;

정답은 SYSDATE () 입니다.

INSERT INTO servers (
    server_name, online_status, exchange, disk_space,
    network_shares, date_time
)
VALUES (
    'm1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE', SYSDATE()
);

sysdate_is_now 명령 행 인수를 로 설정 하여이 동작을 변경하고 MySQL NOW()이 동일한 방식으로 작동하도록 만들 수 있습니다 .SYSDATE()True

그 주 NOW()(갖는 CURRENT_TIMESTAMP()별칭) 상이 SYSDATE()A의 미묘한 방식 :

SYSDATE() returns the time at which it executes. This differs from the behavior for NOW(), which returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger, NOW() returns the time at which the function or triggering statement began to execute.)

As indicated by Erandi, it is best to create your table with the DEFAULT clause so that the column gets populated automatically with the timestamp when you insert a new row:

date_time datetime NOT NULL DEFAULT SYSDATE()

If you want the current date in epoch format, then you can use UNIX_TIMESTAMP(). For example:

select now(3), sysdate(3), unix_timestamp();

would yield

+-------------------------+-------------------------+------------------+
| now(3)                  | sysdate(3)              | unix_timestamp() |
+-------------------------+-------------------------+------------------+
| 2018-11-27 01:40:08.160 | 2018-11-27 01:40:08.160 |       1543282808 |
+-------------------------+-------------------------+------------------+

Related:


You can use not only now(), also current_timestamp() and localtimestamp(). The main reason of incorrect display timestamp is inserting NOW() with single quotes! It didn't work for me in MySQL Workbench because of this IDE add single quotes for mysql functions and i didn't recognize it at once )

Don't use functions with single quotes like in MySQL Workbench. It doesn't work.


$rs = $db->Insert('register',"'$fn','$ln','$email','$pass','$city','$mo','$fil'","'f_name','l_name=','email','password','city','contact','image'");

참고URL : https://stackoverflow.com/questions/19246309/how-to-get-current-date-time-in-mysql

반응형