我有2个表,其中一个包含消息,另一个包含用户。
消息表包含两个ID,即" UserOne"和" UserTwo"同上
我选择其他用户ID(不是已记录的用户ID)并仅通过此代码
获取唯一ID$usersEmails = array();
$query = "SELECT `UserOne`, `UserTwo` FROM `messages` WHERE (`UserOne`='$id' OR `UserTwo`='$id')";
if($query_run = mysqli_query($connect, $query)){
$num_rows = mysqli_num_rows($query_run);
if($num_rows > 0){
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);
}
}
}
}
$loopStop = count($usersEmails);
这里的问题开始了......
我想选择那些使用它们的唯一用户电子邮件我在该数组中插入名为 $ usersEmails
的ID我使用此代码显示唯一身份用户电子邮件:
for($x = $loopStop - 1; $x >= 0; $x--){
$query_two = "SELECT `Email` FROM `signup` WHERE `Id`='$usersEmails[$x]'";
$query_two_run = mysqli_query($connect, $query_two);
$get_two = mysqli_fetch_assoc($query_two_run);
$email = $get_two['Email'];
echo "$email";}
我知道循环一个sql查询语句并不是很好,所以任何想法都非常昂贵吗?
感谢新年快乐
答案 0 :(得分:0)
尝试此查询
SELECT s.`Email` FROM `messages` m
inner join `signup` s on s.`Id`= m.`UserOne`
WHERE m.`UserOne`='$id'
union
SELECT s.`Email` FROM `messages` m WHERE
inner join `signup` s on s.`Id`= m.`UserTwo`
WHERE m.`UserTwo`='$id'