Redian新闻
>
pls help me in this Sql query
avatar
pls help me in this Sql query# Database - 数据库
h*i
1
hi, DB experts. here i have an problem to create this query:
i have a table:
T1 {fieldID, fieldvisitTime, fieldUserID, fieldIP}
now i want:
get the last visit time and its corresponding IP for each userID.
I think it's possible to make this query in one SQL sentence. but how?
thanks in advance!!!!!!!!!!!!
avatar
b*e
2
try to use the aggregrate function such as Max()
and then GROUP BY userID.

【在 h***i 的大作中提到】
: hi, DB experts. here i have an problem to create this query:
: i have a table:
: T1 {fieldID, fieldvisitTime, fieldUserID, fieldIP}
: now i want:
: get the last visit time and its corresponding IP for each userID.
: I think it's possible to make this query in one SQL sentence. but how?
: thanks in advance!!!!!!!!!!!!

avatar
h*i
3
ok. now i am using this query:
select fieldUserID, max(fieldvisitTime) as lastTime, fieldIP from T1 group by
fieldUserID
but the problem is, it will not return the "corresponding fieldIP". simply
it just pick the first IP for each userID group.
:(
so how?
thx

【在 b*e 的大作中提到】
: try to use the aggregrate function such as Max()
: and then GROUP BY userID.

avatar
m*c
4

Should you group by all fields instead of just fieldUserID:
... GROUP BY fieldUserID, lastTime, fieldID
by

【在 h***i 的大作中提到】
: ok. now i am using this query:
: select fieldUserID, max(fieldvisitTime) as lastTime, fieldIP from T1 group by
: fieldUserID
: but the problem is, it will not return the "corresponding fieldIP". simply
: it just pick the first IP for each userID group.
: :(
: so how?
: thx

avatar
n*n
5
试试这个:
select t.fieldUserID, t.fieldvisitTime as lastTime, t.fieldIP
from T1 t, (select fieldUserID, max(fieldvisitTime) as visitTime
from T1 group by fieldUserID) m
where t.fieldUserID = m.fieldUserID
and t.fieldvisitTime = m.visitTime

by

【在 h***i 的大作中提到】
: ok. now i am using this query:
: select fieldUserID, max(fieldvisitTime) as lastTime, fieldIP from T1 group by
: fieldUserID
: but the problem is, it will not return the "corresponding fieldIP". simply
: it just pick the first IP for each userID group.
: :(
: so how?
: thx

avatar
a*c
6
Or this one -
Select fieldUserID, fieldvisittime, fieldIP
from T1 a
where fieldvisittime = (select MAX(fieldvisittime)
from T1 b
where b.fieldUserID = a.fieldUserID)

group
simply

【在 n***n 的大作中提到】
: 试试这个:
: select t.fieldUserID, t.fieldvisitTime as lastTime, t.fieldIP
: from T1 t, (select fieldUserID, max(fieldvisitTime) as visitTime
: from T1 group by fieldUserID) m
: where t.fieldUserID = m.fieldUserID
: and t.fieldvisitTime = m.visitTime
:
: by

avatar
h*i
7
thanks for all of you!
i have never tried to use the "compound" sentence. :)
i'll try and let you know latter.

group
simply

【在 n***n 的大作中提到】
: 试试这个:
: select t.fieldUserID, t.fieldvisitTime as lastTime, t.fieldIP
: from T1 t, (select fieldUserID, max(fieldvisitTime) as visitTime
: from T1 group by fieldUserID) m
: where t.fieldUserID = m.fieldUserID
: and t.fieldvisitTime = m.visitTime
:
: by

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。