Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [SQL] Selezionare il penultimo record

    Salve mediante una tabella di log vorrei selezionare solo il penultimo record accesso in modo tale da ricavare l'ultima visita di un utente esclusa quella correte (la quale sarebbe l'ultima registrata). Come posso modificare questa query base?

    codice:
    SELECT datetimeip 
    FROM log 
    WHERE iduser = 3
    Giuseppe SPECCHIO

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    Mah ... a me e' venuta questa

    SELECT TOP 1 *
    FROM
    (SELECT TOP 2 datetimeip
    FROM log
    WHERE iduser = 2
    ORDER BY datetimeip DESC)
    ORDER BY datetimeip


    ... non e' detto che non ci sia qualcosa di meglio ...

  3. #3
    a me è venuta così, ma forse è la stessa cosa
    Codice PHP:
    select 
    from (select from log order by datetimeip desc limit 2) as L
    order by datetimeip
    limit 1

    ciao
    sergio

  4. #4
    Danno entrabe errore:

    codice:
    Errore
    
    query SQL: Documentazione
    
    SELECT *
    FROM (
    
    SELECT *
    FROM log
    ORDER BY datetimeip DESC
    LIMIT 2
    ) AS L
    ORDER BY datetimeip
    LIMIT 1
    
    Messaggio di MySQL: Documentazione
    #1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select * from log order by datetimeip desc limit 2) as L
    order
    Giuseppe SPECCHIO

  5. #5
    Avendo la definizione della tabella ed il database (che solo nel tuo ultimo post si intuisce essere MySQL) e la versione, potrebbe essere d'aiuto.

    In ogni modo con (PostgreSQL) 8.2.5

    codice:
    -- definizione tabella h_foto
    CREATE TABLE h_foto
    (
      id integer NOT NULL PRIMARY KEY
    )
    la seguente:

    codice:
    select max (id)
    from h_foto
    where id <> (select max (id) from h_foto)
    restituisce il penultimo id più grande.

    Adattala alle tue esigenze.

  6. #6
    Originariamente inviato da peppespe
    Danno entrabe errore:

    codice:
    Errore
    
    query SQL: Documentazione
    
    SELECT *
    FROM (
    
    SELECT *
    FROM log
    ORDER BY datetimeip DESC
    LIMIT 2
    ) AS L
    ORDER BY datetimeip
    LIMIT 1
    
    Messaggio di MySQL: Documentazione
    #1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select * from log order by datetimeip desc limit 2) as L
    order
    su mysql >=5 è giusta, cosa adoperi?
    ciao
    sergio

  7. #7
    La versione di MySQL è la 1.4, e mi sa che la query non và:

    codice:
    Errore
    
    query SQL: Documentazione
    
    SELECT max( datetimeip )
    FROM log
    WHERE datetimeip <> (
    SELECT max( datetimeip )
    FROM log )
    
    Messaggio di MySQL: Documentazione
    #1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near '(datetimeip)
    from log
    where datetimeip <> (select max (dateti
    Giuseppe SPECCHIO

  8. #8
    è tempo di fare un update
    ciao
    sergio

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 © 2024 vBulletin Solutions, Inc. All rights reserved.