0
說如果我想從同一個表中獲得100個不同的用戶,我將不得不創建100個不同的查詢,如下所示。我只以三個查詢爲例,但你明白我的意思。是否有更有效的方法來壓縮這些mysql查詢?
我想知道是否有一個更有效的方法來從同一個表中獲取儘可能多的用戶?重要的是我需要讓每個用戶擁有獨特的句柄。例如。 $ matrix_user_id_2,$ matrix_user_id_3,$ matrix_user_id_4。我怎麼能繼續這樣做呢?
$find_sponsor_2 = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id = :user_id");
$find_sponsor_2->bindValue(':user_id', 2);
$find_sponsor_2->execute();
$result_sponsor_2 = $find_sponsor_2->fetchAll(PDO::FETCH_ASSOC);
if(count($result_sponsor_2) > 0) {
foreach($result_sponsor_2 as $row) {
$matrix_user_id_2 = $row['user_id'];
$filled_positions_2 = $row['filled_positions'];
}
} else {
$errors[] = 'User Id 2 not found in Matrix.';
}
$find_sponsor_3 = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id = :user_id");
$find_sponsor_3->bindValue(':user_id', 3);
$find_sponsor_3->execute();
$result_sponsor_3 = $find_sponsor_3->fetchAll(PDO::FETCH_ASSOC);
if(count($result_sponsor_3) > 0) {
foreach($result_sponsor_3 as $row) {
$matrix_user_id_3 = $row['user_id'];
$filled_positions_3 = $row['filled_positions'];
}
} else {
$errors[] = 'User Id 3 not found in Matrix.';
}
$find_sponsor_4 = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id = :user_id");
$find_sponsor_4->bindValue(':user_id', 4);
$find_sponsor_4->execute();
$result_sponsor_4 = $find_sponsor_4->fetchAll(PDO::FETCH_ASSOC);
if(count($result_sponsor_4) > 0) {
foreach($result_sponsor_4 as $row) {
$matrix_user_id_4 = $row['user_id'];
$filled_positions_4 = $row['filled_positions'];
}
} else {
$errors[] = 'User Id 4 not found in Matrix.';
}
請給我看完整的查詢。這將有所幫助。謝謝。 – tom
@tom寫這個查詢和'print_r'數據集,你會知道我在說什麼 –
抱歉沒有工作。我不能正確理解它。如果你能給我看完整的查詢,那會更好。 – tom