Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 具有最新时间戳的SQL记录,但具有枚举用户的联接,其中没有特定状态_Mysql_Sql_Datetime_Inner Join_Greatest N Per Group - Fatal编程技术网

Mysql 具有最新时间戳的SQL记录,但具有枚举用户的联接,其中没有特定状态

Mysql 具有最新时间戳的SQL记录,但具有枚举用户的联接,其中没有特定状态,mysql,sql,datetime,inner-join,greatest-n-per-group,Mysql,Sql,Datetime,Inner Join,Greatest N Per Group,真的很难匹配其他人的例子在这一个,所以想知道是否有人会足够好,以指出我在正确的方向 我在MySQL中有两个表 标签 使用者 我的过程只是为扫描的tagid向Tags表中添加行,因此可能有许多行具有相同的tagid,但每行具有不同的信息,具体取决于用户,每行具有发生时间的时间戳 问题是我想列出每个tagid的最新记录,但我想排除任何带有Tags.status“store”的内容,并将Tags.createuser枚举到Users.userid的名称中 我只是不知道如何获得最后一个时间戳,以及如何执

真的很难匹配其他人的例子在这一个,所以想知道是否有人会足够好,以指出我在正确的方向

我在MySQL中有两个表

标签

使用者

我的过程只是为扫描的tagid向Tags表中添加行,因此可能有许多行具有相同的tagid,但每行具有不同的信息,具体取决于用户,每行具有发生时间的时间戳

问题是我想列出每个tagid的最新记录,但我想排除任何带有Tags.status“store”的内容,并将Tags.createuser枚举到Users.userid的名称中

我只是不知道如何获得最后一个时间戳,以及如何执行NOT语句,因为可能会出现如下情况

tagid, status, lot, lat, long, createuser, timestamp
1000001, live, 1, xxxx, yyyy, 1, 2020-10-20 12:00
1000001, store, 1, xxxx, yyyy, 1, 2020-10-20 12:10
1000002, live, 1, xxxx, yyyy, 2, 2020-10-20 11:00
用户2=Joe Bloggs

所以我只想返回以下内容,因为1000001的最后一条记录是“store”

1000002, live, 1, xxxx, yyyy, Joe Bloggs, 2020-10-20 11:00

您需要每个标记的最新记录,以及关联的用户名-当且仅当该标记的状态为live时

您可以使用行号和筛选:

select t.*, u.surname
from users u
inner join (
    select t.*, row_number() over(partition by tagid order by timestamp desc) rn
    from tags
) t on t.createduser = u.userid
where t.rn = 1 and t.status = 'live'
select t.*, u.surname
from users u
inner join tags t on t.createduser = u.userid
where t.status = 'live' and t.timestamp = (
    select max(t1.timestamp) from tags t1 where t1.tagid = t.tagid
)
这需要MySQL 8.0。在早期版本中,有一个选项使用相关子查询进行筛选:

select t.*, u.surname
from users u
inner join (
    select t.*, row_number() over(partition by tagid order by timestamp desc) rn
    from tags
) t on t.createduser = u.userid
where t.rn = 1 and t.status = 'live'
select t.*, u.surname
from users u
inner join tags t on t.createduser = u.userid
where t.status = 'live' and t.timestamp = (
    select max(t1.timestamp) from tags t1 where t1.tagid = t.tagid
)

您需要每个标记的最新记录,以及关联的用户名-当且仅当该标记的状态为live时

您可以使用行号和筛选:

select t.*, u.surname
from users u
inner join (
    select t.*, row_number() over(partition by tagid order by timestamp desc) rn
    from tags
) t on t.createduser = u.userid
where t.rn = 1 and t.status = 'live'
select t.*, u.surname
from users u
inner join tags t on t.createduser = u.userid
where t.status = 'live' and t.timestamp = (
    select max(t1.timestamp) from tags t1 where t1.tagid = t.tagid
)
这需要MySQL 8.0。在早期版本中,有一个选项使用相关子查询进行筛选:

select t.*, u.surname
from users u
inner join (
    select t.*, row_number() over(partition by tagid order by timestamp desc) rn
    from tags
) t on t.createduser = u.userid
where t.rn = 1 and t.status = 'live'
select t.*, u.surname
from users u
inner join tags t on t.createduser = u.userid
where t.status = 'live' and t.timestamp = (
    select max(t1.timestamp) from tags t1 where t1.tagid = t.tagid
)

请参阅:。请注意,在本例中,我怀疑仅包含3行的数据集是否有意义地被视为“代表”。请参阅:。请注意,在本例中,我怀疑仅包含3行的数据集是否有意义地被视为“代表性”数据集。好的,所以我有一个阶段可以让时间戳和排除位工作。。。。只是想弄清楚我现在是如何连接到Users表的。。。。从选择t1中选择*。*从标记t1中选择,其中t1.tagtime=从标记t2中选择MAXt2.tagtime,其中t2.tagid=t1.tagid为t,其中状态!=百货商店很抱歉上面的格式设置。。。“真的不知道如何让它更好地为人们服务。”贾索尼尔默斯:我不理解你的评论。以上查询已加入用户表。忽略。。。非常感谢。。。。第二个选项,因为我仍然在使用SQL7,它的工作方式很有魅力。我想我也明白了这个概念-好的,我有一个阶段,可以让时间戳和排除位工作。。。。只是想弄清楚我现在是如何连接到Users表的。。。。从选择t1中选择*。*从标记t1中选择,其中t1.tagtime=从标记t2中选择MAXt2.tagtime,其中t2.tagid=t1.tagid为t,其中状态!=百货商店很抱歉上面的格式设置。。。“真的不知道如何让它更好地为人们服务。”贾索尼尔默斯:我不理解你的评论。以上查询已加入用户表。忽略。。。非常感谢。。。。第二个选项,因为我仍然在使用SQL7,它的工作方式很有魅力。我想我也明白了这个概念-