Mysql实现Oracle row_number() over()
mysql实现row_number()方法
SELECT *
FROM
(
SELECT A.*
,if(account_id = @account_id,@rank:=@rank+1,@rank := l) AS rank
,@account_id := account_id
FROM (SELECT * FROM C order by account_id,id DESC) AS A, (select @account_id = null,@rank := 0) AS B
) t
WHERE t.rank = 1
上面标红的子查询可以以where条件的形式写在后面
SELECT *
FROM
(
SELECT A.*
,if(account_id = @account_id,@rank:=@rank+1,@rank := l) AS rank
,@account_id := account_id
FROM C AS A, (select @account_id = null,@rank := 0) AS B ORDER BY A.account_id,id DESC
) t
WHERE t.rank = 1
SELECT *
FROM
(
SELECT A.*
,if(account_id = @account_id,@rank:=@rank+1,@rank := l) AS rank
,@account_id := account_id
FROM (SELECT * FROM C order by account_id,id DESC) AS A, (select @account_id = null,@rank := 0) AS B
) t
WHERE t.rank = 1
上面标红的子查询可以以where条件的形式写在后面
SELECT *
FROM
(
SELECT A.*
,if(account_id = @account_id,@rank:=@rank+1,@rank := l) AS rank
,@account_id := account_id
FROM C AS A, (select @account_id = null,@rank := 0) AS B ORDER BY A.account_id,id DESC
) t
WHERE t.rank = 1
留言
張貼留言