SQL获取每个用户最近修改的帖子
这是我的桌子:SQL获取每个用户最近修改的帖子,sql,sql-server,database,Sql,Sql Server,Database,这是我的桌子: CREATE TABLE [dbo].[posts] ( [id] [int] IDENTITY(1,1) NOT NULL, [user_id] [int] NOT NULL, [date_posted] [datetime] NOT NULL, [date_modified] [datetime] NOT NULL, [content] [text] NOT NULL, CONSTRAINT [PK_posts] PRIMAR
CREATE TABLE [dbo].[posts]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[user_id] [int] NOT NULL,
[date_posted] [datetime] NOT NULL,
[date_modified] [datetime] NOT NULL,
[content] [text] NOT NULL,
CONSTRAINT [PK_posts] PRIMARY KEY CLUSTERED ( [id] ASC )
)
我的公司需要一个查询,该查询将获取每个用户最近修改的帖子的帖子id。有人能帮我吗?谢谢,函数的rank()
SELECT user_id, id AS most_recent_post_id
FROM (SELECT user_id,
id,
RANK() OVER (PARTITION BY user_id ORDER BY date_posted DESC) AS rk
FROM [posts]) p
WHERE rk = 1
到目前为止你试过什么?你确定你在使用mySQL吗?这在我看来更像是SQL Server语法。哎呀,这就是SQL,这就是为什么我看mySQL,我不知道这个=。=!