我有2个这样的表:
USER
ID_____LastLogin
1------2013-12-19
2------2013-11-12
3------2013-09-14
4------2013-07-18
5------2013-06-13
OTHER
ID_____UserID___TimeStamp
1------1--------2013-12-17
2------2--------2013-10-11
3------3--------2013-05-14
4------4--------2013-02-18
我正在尝试计算自“Other TimeStamp”以来90天后登录的用户数,如果他们在“其他”表中。
期望的结果:
2
所需结果解释:
UserID of '1' LastLogin is not greater than Other.TimeStamp (excluded in count)
UserID of '2' LastLogin is not greater than Other.TimeStamp (excluded in count)
UserID of '3' LastLogin is greater than Other.TimeStamp (included in count)
UserID of '4' LastLogin is greater than Other.TimeStamp (included in count)
UserID of '5' is not in Other table (excluded in count)
我试过了:
SELECT COUNT(DISTINCT(user.ID))
FROM user INNER JOIN other ON other.UserID = User.ID
WHERE DATE(user.LastLogin) >= (
SELECT (DATE(other.TimeStamp) + INTERVAL 90 DAY)
FROM other
GROUP BY other.UserID)
我收到错误消息:
"Subquery returns more than 1 row"
我理解错误显示的原因(我无法比较子查询的多次计数),尽管我正在尝试比较那些用户的数量.LastLogin到同一用户的其他.TimeStamp < / p>
答案 0 :(得分:3)
为什么要使用子查询?
SELECT COUNT(*)
FROM user JOIN other ON other.UserID = user.ID
WHERE user.LastLogin >= other.TimeStamp + INTERVAL 90 DAY
答案 1 :(得分:0)
您可以使用DATEDIFF()
功能:
SELECT COUNT(*)
FROM user
INNER JOIN other ON other.UserID = User.ID
WHERE DATEDIFF(user.Lastlogin,other.TimeStamp)>90