让所有用户首次访问日期

时间:2014-07-17 15:30:03

标签: mysql

我正在尝试获取用户第一个视图的日期和IP地址列表。这个2天的清单(实际数据中会有更多的ip):

"day"           "ip"
"2014-07-01"    "173.8.118.153"
"2014-07-01"    "173.8.118.153"
"2014-07-01"    "173.8.118.153"
"2014-07-02"    "173.8.118.153"
"2014-07-02"    "173.8.118.153"
"2014-07-02"    "173.8.118.153"
"2014-07-02"    "173.8.118.153"
"2014-07-02"    "173.8.118.153"

我想在这个用户的第一天返回2014-07-01。如何在第一天获取所有用户的列表?

我在这里的查询有效,但我知道这不正确,因为如果我将order byasc更改为desc,反之亦然,我仍然会在同一天。基本上这告诉我它正在为这个ip找到它的第一天,并且谁知道数据可能在某些用户的数据库中倒退。我该怎么做才能保证我第一次来访?

select date(viewed) as day, inet_ntoa(ip) as user_ip
from ad_views
where ad_id = 3058440
and viewed > date_sub(now(), interval 30 day)
group by user_ip order by day;

4 个答案:

答案 0 :(得分:2)

Select min(date_col) from table_name

This文档可能对您的搜索有用

答案 1 :(得分:2)

获取日期MIN()

select MIN(date(viewed)) as day, inet_ntoa(ip) as user_ip
from ad_views
where ad_id = 3058440
group by user_ip;

答案 2 :(得分:2)

由于您已经进行了分组,请使用min

viewed
select date(min(viewed)) as day, inet_ntoa(ip) as user_ip
from ad_views
where ad_id = 3058440
and viewed > date_sub(now(), interval 30 day)
group by user_ip order by day;

答案 3 :(得分:0)

SELECT day, ip
FROM MyTable t1 LEFT OUTER JOIN MyTable t2
    ON (t1.ip = t2.ip AND t1.day <= t2.day)