我有这段代码将数据插入名为$usersEmails
的数组中:
while($get = mysqli_fetch_assoc($query_run)){
$userOne = $get['UserOne'];
$userTwo = $get['UserTwo'];
if($userOne != $id){
array_push($usersEmails, $userOne);
}else if($userTwo != $id){
array_push($usersEmails, $userTwo);
}
}
然后我使用这个数组进行了查询:
$query = 'SELECT `Email` FROM `users`
WHERE `Id` IN ('.implode(',', array_map('intval', $usersEmails)).')';
此代码生成users.Id
排序的结果,但我希望这些ID的排序顺序与我们的数组中的顺序相同。
换句话说,我希望从0 --> end of array
答案 0 :(得分:1)
添加到您的SQL查询order by
并使用field
:
$query = 'select `Email`
from `users`
where `Id` in (' . implode(',', array_map('intval', $usersEmails)) . ')
order by field(`Id`, ' . implode(',', array_map('intval', $usersEmails)) . ')