我在mysql中有两个表- User (user_id,first_name ...)和login_history(user_id、login_time)
用户每次登录时,系统都会将时间记录在login_history中。
我想运行一个查询,从users表中获取所有字段,并从login_history中获取最新的登录时间。有谁能帮帮忙吗?
发布于 2017-08-03 22:39:03
SELECT t1.col1
,t1.col2
,[...repeat for all columns in User table]
,max(t2.login_time)
FROM user t1
INNER JOIN login_history t2 ON t1.user_id = t2.user_id
GROUP BY t1.col1
,t1.col2
,[..repeat for all columns in User table]假设login_time以合理的数据类型和/或格式存储,这应该是可行的。
发布于 2017-08-03 22:26:03
然后,您必须使用连接:
SELECT *, login_history.login_time
FROM User
INNER JOIN login_history
ON User.user_id=login_history.user_id;这个查询将给出User和login_time的所有列。
发布于 2017-08-03 22:37:21
以下两个查询可以帮助您选择具有用户详细信息的最新登录时间
SELECT * FROM User C,login_history O where C.user_id=O.user_id order by O.login_time desc limit 1或
SELECT * FROM User C,login_history O where C.user_id=O.user_id and ROWNUM <=1 order by O.login_time deschttps://stackoverflow.com/questions/45486983
复制相似问题