gpt4 book ai didi

php - 按 IN 子句的顺序对记录进行排序

转载 作者:行者123 更新时间:2023-11-28 23:39:38 25 4
gpt4 key购买 nike

我有这段代码可以将数据插入到名为 $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

输出ID

最佳答案

添加到您的 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)) . ')

关于php - 按 IN 子句的顺序对记录进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34749371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com