• User Attivo

    [PHP] Algoritmo per Aste al ribasso

    Ciao, sto cercando di creare un piccolo script per le aste al ribasso, ma non so' come rendere l'algoritmo il più veloce possibile.

    L'argotimo deve vedere se l'offerta dell'utente ($user_bid) è l'offerta unica più bassa presente nel db.

    Il db è strutturato così:
    id | user_bid | item | bid


  • Super User

    SELECT count(id) AS tot, * FROM table GROUP BY user_bid, bid HAVING tot=1 ORDER BY bid LIMIT 1

    Questo ti restituisce l'offerta unica più bassa.
    e ti interessa per un singlo utente basta aggiungere WHERE user_bid="miouser"
    Se non ti restituisce niente allora non è l'offerta unica piu bassa.


  • User Attivo

    In un'altro forum mi hanno suggerito:

    function offertaVincente( $user_bid, $item ){

    $query = "SELECT * from nomeTabella AS T1
    WHERE T1.user_bid = $user_bid AND T1.item = $item
    AND T1.bid <= ALL ( SELECT T2.bid FROM nometabella AS T2
    WHERE T1.item = T2.item AND T1.user_bid != T2.user_bid ) //è lofferta più bassa fatta sul determinato oggetto
    AND NOT EXISTS ( SELECT * from nometabella AS T3 //ed è anche l'unica
    WHERE T1.item = T3.item AND T3.bid < T1.bid );";

    $res = mysql_query($query);

    if ( mysql_affected_rows( $res ) > 0 )
    return true;

    return false;

    }

    Quale delle due potrebbe essere più soddisfacente?


  • User Attivo

    Non mi funziona. Mi dà errore di sintassi.

    up