让我说我有数据......
ID ... 1,2,3,4,5,6
FirstName ... Donald,Tabatha,Jeremy,Samuel,Donald,Tabatha
LastName ... Faulknor,Kolasa,Jones,Jackson,Faulknor,Kolasa
在这个例子中,我想要显示 Donald Faulknor,Tabatha Kolasa,Jeremy Jones,Samuel Jackson,
我可以显示所有数据(重复重复的条目),或者,如果我使用
SELECT DISTINCT,那么我将只显示第一个实例,在这种情况下将是Donald Faulknor。这是我的代码......
$sql55 = "SELECT * FROM messages WHERE lowerID = :lowerIDb || higherID = :higherIDb"; $stmt55 = $pdo->prepare($sql55); $stmt55->bindValue(':lowerIDb',$user_id); $stmt55->bindValue(':higherIDb',$user_id); $stmt55->execute(); while($row55 = $stmt55->fetch(PDO::FETCH_ASSOC)) { $lowerID55 = $row55['lowerID']; $higherID55 = $row55['higherID']; if($lowerID55 == $user_id) { $user_id55 = $higherID55; } elseif($higherID55 == $user_id) { $user_id55 = $lowerID55; } $sql56 = "SELECT DISTINCT user_id FROM users WHERE user_id = :user_id55"; $stmt56 = $pdo->prepare($sql56); $stmt56->bindValue(':user_id55',$user_id55); $stmt56->execute(); while($row56 = $stmt56->fetch(PDO::FETCH_ASSOC)) { $id56 = $row56['user_id']; $sql58 = "SELECT * FROM users WHERE user_id = :user_id58"; $stmt58 = $pdo->prepare($sql58); $stmt58->bindValue(':user_id58',$id56); $stmt58->execute(); while($row58 = $stmt58->fetch(PDO::FETCH_ASSOC)) { $firstname56 = $row58['firstname']; $lastname56 = $row58['lastname']; $profilePhoto56 = $row58['profilePhoto']; $sql57 = "SELECT * from photos WHERE id = :profilePhotoa"; $stmt57 = $pdo->prepare($sql57); $stmt57->bindValue(':profilePhotoa',$profilePhoto56); $stmt57->execute(); while($row57 = $stmt57->fetch(PDO::FETCH_ASSOC)) { $profilePhotoPath57 = $row57['path']; echo ''; echo ' '.$firstname56.' '.$lastname56.''; } } }}
为了更好地理解上面的代码......我有来自用户的消息,当然,来自同一个用户的消息会很多。我试图让他们的名字(收件人)在侧栏显示一次(表示来自/向该人传达的信息。比方说,我有20封来自一个人的消息。我可以得到这个名字给重复二十次(不使用DISTINCT)或者我只能显示一个人的名字(使用DISTINCT)。所以,如果有8个人向该用户发送电子邮件,则只会显示一个名称。
非常感谢很多帮助。如有问题或详细说明,请随时提问。谢谢! :)