Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL, PHP:seleziona * dalla tabella in cui l'id non è nell'array

Nel caso $all è l'array da cui vuoi estrarre gli ID indesiderati, questo potrebbe essere ciò di cui hai bisogno dopo il codice che hai fornito:

$ids_to_exclude = array();

// iterate through servers
foreach ($all as $server_id => $dates) {
    // iterate through dates of each server
    foreach ($dates as $date => $id) {
        // If a value is not in the array, add it.
        // In case ids don't repeat, you won't need this if
        if (!in_array($id, $ids_to_exclude)) {
             // add $id to the array
             $ids_to_exclude[] = $id;
        }
    }
}

$sql_condition = "where `id` not in (".implode(",",$ids_to_exclude).")";

Fai solo attenzione quando scrivi query con concatenazione di stringhe. Leggi di SQL Injection e come prevenirlo. Usa Dichiarazioni preparate invece della pura concatenazione.