gpt4 book ai didi

MySQL 子查询/查询问题

转载 作者:行者123 更新时间:2023-11-29 07:37:37 26 4
gpt4 key购买 nike

我对这个查询有心理障碍,我试图返回最大日期和最大时间,并根据身份进行排序。如果有人能为这种类型的查询添加一双眼睛,我们将不胜感激所以:

数据集

Identity, Date, Time, Website
10, 5/10/15, 1, google.com
10, 5/10/15, 3, google.com
10, 5/10/15, 10, google.com
25, 5/11/15, 1, yahoo.com
25, 5/11/15, 15, yahoo.com

预期结果

10, 5/10/15, 10, google.com  
25, 5/11/15, 15, yahoo.com

当前查询

SELECT DISTINCT *, MAX(datetime) as maxdate, MAX(time), identity
FROM identity_track
GROUP BY identity
ORDER BY maxdate DESC

最佳答案

类似这样的吗?

select identity, max(date), max(time), website
from identity_track
group by website;

此处演示:http://sqlfiddle.com/#!9/5cadf/1

您可以按您想要的任何字段进行排序。

此外,您发布的预期输出与您尝试执行的操作并不相符。

编辑

根据附加信息更新了查询。

select t.identity, t.date, max(t.time), t.website
from t
inner join
(select identity, website, max(date) d
from t
group by identity, website) q
on t.identity = q.identity
and t.website = q.website
and q.d = t.date
group by t.identity, t.website, t.date

这应该为您提供用户身份、他访问的页面、他上次访问该页面的时间以及他上次访问的任何访问所花费的最长时间。

关于MySQL 子查询/查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227886/

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