Sto cercando di visualizzare i primi 15 giocatori della mia classifica ma voglio anche assicurarmi che il giocatore che ha richiesto la classifica sia mostrato, con una sola richiesta SQL
Consente di ridurre la scheda solo a 4 giocatori.
Immagina che io (chi ha richiesto di vedere il tabellone) classificato al 2 ° posto, il tabellone dovrebbe mostrare quanto segue
pos,name
1,bob
2,ME
3,tony
4,andy
Ma immaginiamo di essere classificato al 150esimo, quindi il consiglio dovrebbe venire come
pos,name
1,bob
2,tony
3,andy
150,ME
Sto usando la seguente richiesta:
SELECT * FROM
(
SELECT name,rating,
(SELECT COUNT(*)
FROM rank AS t2
WHERE t2.rating > t1.rating)
AS position FROM rank AS t1
LIMIT 15
)
UNION
SELECT name,rating,(SELECT COUNT(*)
FROM rank AS t2
WHERE t2.rating > t1.rating)
AS position FROM rank AS t1
WHERE t1.name = "ben"
ORDER BY rating DESC
Se sono classificato tra i primi 15, la scheda mostra correttamente. Ma se ho superato il primo 15, la query restituisce 16 righe con me che è l'ultima
In breve, voglio mostrare 15 giocatori in classifica ma uno di loro DEVE essere me
modifica: si noti che due giocatori possono trovarsi nella stessa posizione in classifica
risposte:
0 per risposta № 1SELECT * FROM
(
SELECT name,rating,
(SELECT COUNT(*)
FROM rank AS t2
WHERE t2.rating > t1.rating
AND t1.name <> "ben")
AS position FROM rank AS t1
LIMIT 14
)
UNION
SELECT name,rating,(SELECT COUNT(*)
FROM rank AS t2
WHERE t2.rating > t1.rating)
AS position FROM rank AS t1
WHERE t1.name = "ben"
ORDER BY rating DESC
Qui, invece, non restituire il proprio utente nella prima query e limitarlo a 14. Quindi nella seconda query restituire solo il proprio utente.