get_var($wpdb->prepare("SELECT MIN(sort_order) FROM ".WATU_QUESTIONS." WHERE exam_id=%d AND ID IN (".implode(',', $qids).")", $exam_id)); // go through the questions and increment the min for each of them foreach($qids as $qid) { if($qid == -1) continue; $wpdb->query($wpdb->prepare("UPDATE ".WATU_QUESTIONS." SET sort_order=%d WHERE ID=%d", $min_sort_order, $qid)); $min_sort_order++; } }