2012-07-12 78 views
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 

如果用户登录。如何计算用户位置?基地他的信息发表多少票有

回答

0

试试这个::

Select rank, temp_p.userID from 
(
select @rownum:[email protected]+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 
+0

这给出了一个语法错误 – 2012-07-12 17:20:22

+0

什么错误? – 2012-07-12 17:40:33

+0

为正确的语法使用附近'ORDER BY count DESC)作为temp_p WHERE p.iduser = 39'在第13行 – 2012-07-12 17:55:39

相关问题