CURRENT_TIMESTAMP (밀리 초)
타임 스탬프에서 MySql
또는 PostgreSql
(또는 호기심에서 벗어나는 다른 사람) 에서 밀리 초를 얻을 수있는 방법이 있습니까?
SELECT CURRENT_TIMESTAMP
--> 2012-03-08 20:12:06.032572
다음과 같은 것이 있습니까?
SELECT CURRENT_MILLISEC
--> 1331255526000
또는 유일한 대안은을 사용하는 것입니다 DATEDIFF
으로부터 era
?
MySQL 에서 Unix 타임 스탬프 를 초 단위 로 가져 오려면 :
select UNIX_TIMESTAMP();
세부 정보 : http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
테스트 된 PostgreSQL은 아니지만이 사이트에 따르면 작동합니다. http://www.raditha.com/postgres/timestamp.php
select round( date_part( 'epoch', now() ) );
MySQL (5.6 이상)의 경우 다음을 수행 할 수 있습니다.
SELECT ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000)
반환되는 항목 (예) :
1420998416685 --milliseconds
mysql에서는 uuid 함수를 사용하여 밀리 초를 추출 할 수 있습니다.
select conv(
concat(
substring(uid,16,3),
substring(uid,10,4),
substring(uid,1,8))
,16,10)
div 10000
- (141427 * 24 * 60 * 60 * 1000) as current_mills
from (select uuid() uid) as alias;
결과:
+---------------+
| current_mills |
+---------------+
| 1410954031133 |
+---------------+
이전 mysql 버전에서도 작동합니다!
이 페이지에 감사드립니다 : http://rpbouman.blogspot.com.es/2014/06/mysql-extracting-timstamp-and-mac.html
Mysql 5.7 이상에서는 다음을 실행할 수 있습니다.
select current_timestamp(6)
자세한 사항은
https://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html
현재 문서 에 따라 PostgreSQL의 타임 스탬프 값에서 밀리 초를 추출하는 올바른 방법 은 다음과 같습니다.
SELECT date_part('milliseconds', current_timestamp);
--OR
SELECT EXTRACT(MILLISECONDS FROM current_timestamp);
반환 포함 : 분수 부분을 포함한 초 필드에 1000을 곱한 값입니다. 여기에는 전체 초가 포함됩니다.
타임 스탬프가있는 MySQL의 주요 오해는 기본적으로 MySQL이 분수 부분없이 타임 스탬프를 반환하고 저장한다는 것 입니다.
SELECT current_timestamp() => 2018-01-18 12:05:34
초 타임 스탬프로 변환 할 수 있습니다.
SELECT UNIX_TIMESTAMP(current_timestamp()) => 1516272429
분수 부분을 추가하려면 :
SELECT current_timestamp(3) => 2018-01-18 12:05:58.983
마이크로 초 타임 스탬프로 변환 할 수 있습니다.
SELECT CAST( 1000*UNIX_TIMESTAMP(current_timestamp(3)) AS UNSIGNED INTEGER) ts => 1516272274786
테이블에 저장하는 데는 몇 가지 트릭이 있습니다. 테이블이 다음과 같이 생성 된 경우
CREATE TABLE `ts_test_table` (
`id` int(1) NOT NULL,
`not_fractional_timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
MySQL은 그 안에 소수 부분을 저장하지 않습니다.
id, not_fractional_timestamp
1, 2018-01-18 11:35:12
테이블에 분수 부분을 추가하려면 다른 방법으로 테이블을 만들어야합니다.
CREATE TABLE `ts_test_table2` (
`id` int(1) NOT NULL,
`some_data` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
`fractional_timestamp` timestamp(3) NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
필요한 결과로 이어집니다.
id, some_data, fractional_timestamp
1, 8, 2018-01-18 11:45:40.811
current_timestamp () 함수는 최대 6 개의 값을받을 수 있지만 (적어도 Windows에 설치된 MySQL 5.7.11 버전에서는) 분수 정밀도 6이 꼬리에서 3 자리의 동일한 상수 값으로 이어진다는 것을 알게되었습니다. 제 경우에는 688
id, some_data, fractional_timestamp
1, 2, 2018-01-18 12:01:54.167688
2, 4, 2018-01-18 12:01:58.893688
즉, MySQL의 실제로 사용 가능한 타임 스탬프 정밀도는 플랫폼에 따라 다릅니다.
- Windows : 3
- Linux : 6
사용하다:
Select curtime(4);
이것은 당신에게 밀리 초를 줄 것입니다.
PostgreSQL에서는 다음을 사용할 수 있습니다.
SELECT extract(epoch from now());
MySQL에서 :
SELECT unix_timestamp(now());
다음은 MariaDB 및 MySQL> = 5.6에서 작동하는 표현식입니다.
SELECT (UNIX_TIMESTAMP(NOW()) * 1000000 + MICROSECOND(NOW(6))) AS unix_now_in_microseconds;
This relies on the fact that NOW() always returns the same time throughout a query; it's possible that a plain UNIX_TIMESTAMP()
would work as well, I'm not sure based on the documentation. It also requires MySQL >= 5.6 for the new precision argument for NOW()
function (MariaDB works too).
None of these responses really solve the problem in postgreSQL, i.e :
getting the unix timestamp of a date field in milliseconds
I had the same issue and tested the different previous responses without satisfying result.
Finally, I found a really simple way, probably the simplest :
SELECT (EXTRACT (EPOCH FROM <date_column>::timestamp)::float*1000 as unix_tms
FROM <table>
namely :
- We extract the pgSQL EPOCH, i.e. unix timestamp in floatting seconds from our column casted in timestamp prudence (in some complexe queries, pgSQL could trow an error if this cast isn't explicit. See )
- then we cast it in float and multiply it by 1000 to get the value in milliseconds
Easiest way I found to receive current time in milliseconds in MySql:
SELECT (UNIX_TIMESTAMP(NOW(3)) * 1000)
Since MySql 5.6.
I felt the need to continue to refine, so in MySQL:
Current timestamp in milliseconds:
floor(unix_timestamp(current_timestamp(3)) * 1000)
Timestamp in milliseconds from given datetime(3):
floor(unix_timestamp("2015-04-27 15:14:55.692") * 1000)
Convert timestamp in milliseconds to datetime(3):
from_unixtime(1430146422456 / 1000)
Convert datetime(3) to timestamp in milliseconds:
floor(unix_timestamp("2015-04-27 14:53:42.456") * 1000)
In MariaDB you can use
SELECT NOW(4);
To get milisecs. See here, too.
In PostgreSQL we use this approach:
SELECT round(EXTRACT (EPOCH FROM now())::float*1000)
I faced the same issue recently and I created a small github project that contains a new mysql function UNIX_TIMESTAMP_MS()
that returns the current timestamp in milliseconds.
Also you can do the following :
SELECT UNIX_TIMESTAMP_MS(NOW(3))
or SELECT UNIX_TIMESTAMP_MS(DateTimeField)
The project is located here : https://github.com/silviucpp/unix_timestamp_ms
To compile you need to Just run make compile
in the project root.
Then you need to only copy the shared library in the /usr/lib/mysql/plugin/
(or whatever the plugin folder is on your machine.)
After this just open a mysql console and run :
CREATE FUNCTION UNIX_TIMESTAMP_MS RETURNS INT SONAME 'unix_timestamp_ms.so';
I hope this will help, Silviu
Do as follows for milliseconds:
select round(date_format(CURTIME(3), "%f")/1000)
You can get microseconds by the following:
select date_format(CURTIME(6), "%f")
참고URL : https://stackoverflow.com/questions/9624284/current-timestamp-in-milliseconds
'IT TIP' 카테고리의 다른 글
스톱워치 틱을 나노초, 밀리 초 및 초로 어떻게 변환합니까? (0) | 2020.12.02 |
---|---|
Mac 또는 Mac OS X에서 localhost 폴더는 어디에 있습니까? (0) | 2020.12.02 |
PHP 대 템플릿 엔진 (0) | 2020.12.02 |
Internet Explorer는 일부 도메인의 쿠키를 무시합니다 (쿠키를 읽거나 설정할 수 없음) (0) | 2020.12.02 |
하위 문자열 발생을 계산하는 방법은 무엇입니까? (0) | 2020.12.02 |