0

**Posts:**
| id | userID | title |

| 1  |    6   | text1 |
| 2  |    5   | text2 |
| 3  |    8   | text3 |


**Votes:**
    | id | userID | postID |

    | 1  |    6   |   2    |
    | 2  |    5   |   2    |
    | 3  |    8   |   1    |
    | 4  |    8   |   3    |
    | 5  |    8   |   2    |



**Sql**

SELECT p.*,(SELECT count(*) FROM votes AS v WHERE v.postID=p.id AS count)
FROM posts AS p WHERE p.userID = 6
ORDRER BY count DESC

这将成为前 3 名列表,但如果列表中有类似的内容,我希望名列前茅

Your position is 3

如果用户登录。如何计算用户位置?基于他的帖子有多少票

4

1 回答 1

0

尝试这个 ::

Select rank, temp_p.userID from
(
select @rownum:=@rownum+1 ‘rank’, userId, count(1) as count
from
 (SELECT @rownum:=0) r, 
 votes v 
 inner join posts p on (v.postID=p.id)

 ) temp_p
ORDER BY temp_p.count DESC 
WHERE temp_p.userID.userID = 6
于 2012-07-12T16:32:51.657 回答