gpt4 book ai didi

mysql - 将 foreach 列表排序为 unix 时间戳

转载 作者:行者123 更新时间:2023-11-30 21:31:48 24 4
gpt4 key购买 nike

我有这个来自 mysql 的输出,我想首先排序到最高的 unix 时间戳。 ctime变量是unix时间戳

我在我的代码中添加了一个 foreach i $output {

bind pub "-|-" !grptop add:grptop
proc add:grptop {nick host handle channel text} {

global mysql

set output [AA BB CC DD EE FF]
foreach i $output {
set sql "SELECT * FROM name WHERE grp = '$i' ORDER BY ctime DESC LIMIT 1"
set result [mysqlsel $mysql $sql -list]
set record [lindex $result 0];
set name [lindex $record 2];
set ctime [lindex $record 6];
set date [clock format [lindex [split $ctime] 0] -format {%d:%m:%Y}];
putnow "PRIVMSG $channel :\00314\[\00307$i\00314\]\00300 >\00314 [getLongTime $ctime] \00300> \00314$name"
}
}

proc getLongTime {ctime} {
set date [clock format [lindex [split $ctime] 0] -format {%d:%m:%Y}];
set elapsed [duration [expr [clock seconds] - $ctime]];
regsub -all { second(s)?} $elapsed s elapsed;
regsub -all { minute(s)?} $elapsed m elapsed;
regsub -all { hour(s)?} $elapsed h elapsed;
regsub -all { day(s)?} $elapsed d elapsed;
regsub -all { week(s)?} $elapsed w elapsed;
regsub -all { month(s)?} $elapsed m elapsed;
regsub -all { year(s)?} $elapsed y elapsed;
return $elapsed;
}

输出它的:

<testbot> [AA] > 1d 3h 37m 41s > testname1
<testbot> [CC] > 1y 17w 2d 7h 25m 16s > testname2
<testbot> [DD] > 2h 45m 7s > testname3
<testbot> [BB] > 1d 21h 57m 15s > testname4
<testbot> [EE] > 42m 40s > testname5

并没有及时排序

最佳答案

正如我在评论中所解释的,这是由于查询 (SELECT) 未按照您的预期运行:

SELECT * FROM name WHERE grp = '$i' ORDER BY ctime DESC LIMIT 1

这会为每个组 (grp) 返回最近的 (ctime),但它不提供跨组的排序。为此,并根据您的确切模式,您必须将查询重写为 sth。喜欢:

SELECT n1.*
FROM name n1
WHERE ctime = (SELECT MAX(n2.ctime)
FROM name n2
WHERE n2.grp = n1.grp);
  • 内部 SELECT 获取给定 grp 的最新(最高、最大)时间戳。
  • 外部 SELECT 获取与给定组的最新(最高、最大)时间戳匹配的条目的详细信息。
  • 您可能还想将 LIMIT 1 添加到外部 SELECT,以防您的数据作为每个时间戳(时间戳粒度)的多个条目。

这与 tcl 无关,但是,这并不是说您不能在 Tcl 本身中进行分组/过滤。

关于mysql - 将 foreach 列表排序为 unix 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55816015/

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