我想组合两个表,所以我有用户数据和实际数据以及用户的统计数据。我合并了两个表,所以我得到一个输出。
SELECT users.username, data.followers, data.following, data.pins, users.url,
users.created, data.avatar, data.bio, data.date
FROM users
INNER JOIN data
ON data.userid=users.ID
ORDER BY data.followers DESC
在该数据中,我希望每个用户只显示最新记录,具体取决于最新日期。希望有人可以帮助我。提前致谢!
答案 0 :(得分:0)
您只需加入已分组的查询,即可按用户选择最长日期。
SELECT users.username, data.followers, data.following, data.pins, users.url,
users.created, data.avatar, data.bio, data.date
FROM users
JOIN (select userid, max(date) as maxdate
from data
group by userid
) as md ON md.userid = users.id
JOIN data ON md.userid = data.userid and md.maxdate = data.date
ORDER BY data.followers DESC
我有一些错误,我修好了......这里有工作http://sqlfiddle.com/#!9/2e7ad/2