gpt4 book ai didi

php - 如何创建带有毫秒的Mysql unix_timestamp

转载 作者:行者123 更新时间:2023-11-30 00:23:10 26 4
gpt4 key购买 nike

我有一个php函数使用mysql查询UNIX_TIMESTAMP()来创建唯一的事务号,但我发现如果该函数在同一时间循环,它将为情况1生成相同的事务号,使用情况2则不是大循环的好方法。

1.我使用交易编号是为了在表格列表中对相同的交易日期进行排序。

2.我的事务表使用UUID作为PK,因为数据跨DB。

期望的答案如下所示并且是唯一的。

TransNo = 1397533879.20290

PHP 版本 5.3

Mysql版本5.5

例如函数循环3次

案例1:

SELECT UNIX_TIMESTAMP() as TransNo;

Output :
1397533879
1397533879
1397533879

情况2:

SELECT UNIX_TIMESTAMP() as TransNo, SLEEP(1);

Output :
1397533879
1397533880
1397533881

最佳答案

Desire answer is like below and unique.

TransNo = 1397533879.20290

如果您以微秒部分的精度调用current_timestampnow函数,那么它们会返回timestamp其中包括当前的微秒时间。请注意,允许的最大精度仅适用于 6 位。

请参阅 Fractional Seconds in Time Values 上的文档

示例 1:

select 
@ts:=current_timestamp( 3 ) cts_with_milli_seconds
, unix_timestamp( @ts ) uts_with_ts_milli_seconds;

结果 1:

+-------------------------+---------------------------+
| cts_with_milli_seconds | uts_with_ts_milli_seconds |
+-------------------------+---------------------------+
| 2014-04-15 10:22:17.764 | 1397537537.764000 |
+-------------------------+---------------------------+

示例 2:

select 
@nw:=now( 6 ) now_with_milli_seconds
, unix_timestamp( @nw ) uts_with_nw_milli_seconds;

结果 2:

+----------------------------+---------------------------+
| now_with_milli_seconds | uts_with_nw_milli_seconds |
+----------------------------+---------------------------+
| 2014-04-15 10:22:17.789248 | 1397537537.789248 |
+----------------------------+---------------------------+

旁注:
如果微秒部分仍然匹配,那么您可以将 RAND() 附加到您的值中。

使用RAND()不能保证排序顺序。因此,除非您只需要唯一输出,否则不要使用它。

关于php - 如何创建带有毫秒的Mysql unix_timestamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23074618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com