我有這個疑問排名MySQL的具體ID
SELECT concours_photo_like.id , COUNT(concours_photo_like.id) AS nb_like , (@rank := @rank + 1) AS rank
FROM (SELECT @rank := 0) as r , concours_photo_like
JOIN concours_photo ON concours_photo.id = concours_photo_like.id_concours_photo
WHERE concours_photo.id_concours = 67
GROUP BY concours_photo_like.id_concours_photo
這裏的結果
| id | nb_like | rank |
|--------|------------|------|
| 110 | 2 | 2 |
| 104 | 3 | 1 |
| 134 | 1 | 4 |
| 176 | 1 | 5 |
| 113 | 2 | 3 |
我如何獲得例如特定標識的等級,如果我想的ID 134等級
我想是這樣
SELECT concours_photo_like.id , COUNT(concours_photo_like.id) AS nb_like , (@rank := @rank + 1) AS rank
FROM (SELECT @rank := 0) as r , concours_photo_like
JOIN concours_photo ON concours_photo.id = concours_photo_like.id_concours_photo
WHERE concours_photo.id_concours = 67
AND concours_photo_like.id = 134
GROUP BY concours_photo_like.id_concours_photo
但結果是
| id | nb_like | rank |
|--------|------------|------|
| 134 | 1 | 1 |
,結果應爲等級爲4而不是1
如果你喜歡,可以考慮下列行爲這個簡單的兩步過程:1.如果您還沒有這樣做,提供適當的CREATE和INSERT語句(和/或sqlfiddle),以便我們可以更容易地複製問題。 2.如果您尚未這樣做,請提供與步驟1中提供的信息相對應的所需結果集。 – Strawberry