从查询中选择id大于最小id的行

时间:2013-10-09 07:49:19

标签: sql sql-server

假设我想得到logIduserIdupdatedupdatedById,其中logId大于找到的最低logId这个userId

我试过了:

SELECT * FROM 
(
SELECT logId, userId, updated, updatedById
FROM users_updatelog
WHERE userid=548
) AS tbl1 
WHERE (tbl1.logId > (SELECT MIN(tbl1.logId) FROM tbl1))
ORDER BY tbl1.updated DESC

但这不对。我无法从subqueried表tbl1查询(错误消息:无效的对象名称't​​bl1')

感谢任何提示。

2 个答案:

答案 0 :(得分:4)

SELECT logId, userId, updated, updatedById
FROM users_updatelog
WHERE userid = 548
AND logId > (SELECT min(logId) FROM users_updatelog WHERE userid = 548)
ORDER BY updated DESC

答案 1 :(得分:1)

SELECT logId, userId, updated, updatedById
FROM users_updatelog
WHERE userid=548
AND logId > (SELECT MIN(logId) FROM users_updatelog WHERE userid=548)
ORDER BY updated DESC