2014-07-17 34 views
0

我正在嘗試獲取用戶首次查看的日期和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 byascdesc,反之亦然,我仍然得到同樣的一天。基本上,這告訴我它正在獲得該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; 

回答

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
Select min(date_col) from table_name 

This文件可以證明是有益的搜索

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

既然你已經編組,採取viewedmin

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; 
相關問題