Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2005
    Messaggi
    714

    [MySQL] - Conoscere la posizione nel $row

    Ciao a tutti.
    Non so se sia realmente possibile. Io cmq lo chiedo, magari qualche guro di MySql conosce la risposta.

    Ho una tabella strutturata cosi:

    id - punteggio
    1, 50
    2, 20
    3, 40
    4, 100
    5, 76
    6, 11

    ecc. ecc.

    Per ottenere una classifica, dove chi ha il punteggio piu' alto e' primo fare un ORDER BY punteggio DESC. Ma se io volessi sapere CON UNA SOLA QUERY, la posizione dell'id 3, rispetto all'ordinamento per punteggio desc?

    Ad occhio, ha 40 punti, quindi è 4 in classifica.

    Ma saperlo con una query? E' possibile? Pensando che ovviamente le righe possono essere decine di migliaia.

  2. #2
    Utente di HTML.it L'avatar di bubi1
    Registrato dal
    Dec 2009
    Messaggi
    1,230
    cos'e' un "guro di MySql" ?

    ad ogni modo, se ho capito bene, ti basta fare una cosa del genere:

    select count(distinct punteggio)+1 as posizione from tabella where punteggio > (select punteggio from tabella where id = 3)

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2005
    Messaggi
    714
    mmm, mi ritorna sempre 2 come risultato

  4. #4
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    codice:
    select count(*) + 1 as posizione from 
        (select * from tabella where punteggio > 
           (select punteggio from tabella where id=3) order by punteggio desc) as tab;

  5. #5
    Utente di HTML.it L'avatar di bubi1
    Registrato dal
    Dec 2009
    Messaggi
    1,230
    strano, a me funziona bene, ho riprodotto il tutto per curiosita':

    codice:
    $ /usr/local/mysql5/bin/mysql -p
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 18
    Server version: 5.1.36 Source distribution
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> use test;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    
    mysql> create table tabella (id int, punteggio int) engine myisam;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> insert into tabella values (1,50),(2,20),(3,40),(4,100),(5,76),(6,11);
    Query OK, 6 rows affected (0.00 sec)
    Records: 6  Duplicates: 0  Warnings: 0
    
    mysql> select count(distinct punteggio)+1 as posizione from tabella where punteggio > (select punteggio from tabella where id = 3);
    +-----------+
    | posizione |
    +-----------+
    |         4 |
    +-----------+
    1 row in set (0.00 sec)
    
    mysql> select count(*) + 1 as posizione from      (select * from tabella where punteggio >         (select punteggio from tabella where id=3) order by punteggio desc) as tab;
    +-----------+
    | posizione |
    +-----------+
    |         4 |
    +-----------+
    1 row in set (0.00 sec)
    come si puo' vedere, sia la mia query che quella di nicola danno lo stesso risultato.

    che versione di mysql avete?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.