uid timestamp
1 1242420497
1 1243534661
1 1243534858
1 1243611312
1 1243611511
3 1244817764
3 1244819093
1 1244749446
我有这张桌子,我想抓住时间戳最高的那一行。 我尝试使用
SELECT uid,max(timestamp) FROM `node_revisions` WHERE nid=51
但是返回了
uid timestamp
1 1244819093
你可以看到有错误的uid。如何让它从正确的行中抓取uid?感谢
答案 0 :(得分:5)
你错过了GROUP BY子句。
SELECT
uid,
max(timestamp) as max_time
FROM
node_revisions
WHERE
nid = 51
GROUP BY
uid
ORDER BY
max_time DESC
LIMIT 1
答案 1 :(得分:5)
SELECT * FROM node_revisions
WHERE nid = 51 ORDER BY timestamp DESC LIMIT 1
答案 2 :(得分:2)
首先,nid
未出现在您的表格中。
第二,我怀疑你想要group by uid
答案 3 :(得分:2)
您的示例使用nid = 51而不是uid = 51。此代码是否直接从您正在运行的代码中复制?如果有一个nid字段,这可能是您的问题。你需要一个group by子句。
SELECT uid, max(timestamp) as max_time
FROM 'node_revisions'
WHERE uid = 51
GROUP BY uid
答案 4 :(得分:1)
这样就可以了。确定
select uid FROM `node_revisions`
WHERE uid=51 and timestamp = (select max(timestamp) where uid = 51)
group by uid
答案 5 :(得分:-1)
SELECT uid, timestamp
FROM node_revisions
WHERE timestamp = (SELECT MAX(timestamp) FROM node_revisions);
根据Ryan Oberoi的评论更新了;因为我们只得到一条记录,所以外部查询中的MAX()是不必要的。感谢。