doufang2023 2010-11-04 04:39
浏览 92
已采纳

删除出现在另一个查询中的1个查询的结果

I have a query that retrieves the name of each friend a user has by joining that of friends and users tables. I have another table that stores active users. I need to retrieve friends that are active and not active but for some reason I am drawing a blank. If I have a list of all friends and a list of active friends, can I subtract active from all to be left with offline? All I Want to do basically is have two tabs. Under one will be offline friends. Under the other will be online friends. If anyone has any useful suggestions, I would appreciate it.

 $sql = 'SELECT * FROM users
LEFT JOIN friendships
ON friendships.friend_id = users.id
WHERE friendships.user_id = ?';
$stmt5 = $conn->prepare($sql);
$result=$stmt5->execute(array($userid));

$count=$stmt5->rowCount();

//user has more than 0 friends
if ($count>0){
    while ($row = $stmt5->fetch(PDO::FETCH_ASSOC)) {
    $online=htmlspecialchars( $row['username'], ENT_NOQUOTES, 'UTF-8' );
//check whos online
 $sql = 'SELECT * FROM active_users
WHERE username=?';
$stmt7 = $conn->prepare($sql);
$result=$stmt7->execute(array($online));

$count=$stmt7->rowCount();
while ($row = $stmt7->fetch(PDO::FETCH_ASSOC)) {
$activeuser=$row['username'];
}
}

This code just retrieves active users but hopefully gives an idea of structure.

  • 写回答

2条回答 默认 最新

  • dongye8110 2010-11-04 04:55
    关注

    Could you do a "not in" clause? Without knowing the layout of your database, I'm thinking something like this:

    SELECT * FROM users
    LEFT JOIN friendships
    ON friendships.friend_id = users.id
    WHERE friendships.user_id = ?
    AND users.id NOT IN (
      SELECT user_id FROM active_users
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 复现论文:matlab仿真代码编写
  • ¥15 esp32驱动GC9A01循环播放视频
  • ¥15 惠普360g9的最新bios
  • ¥30 这个功能用什么软件发合适?
  • ¥60 微信小程序,取消订单,偶尔订单没有改变状态
  • ¥15 用pytorch实现PPO算法
  • ¥15 关于调制信号的星座图?
  • ¥30 前端传参时,后端接收不到参数
  • ¥15 这是有什么问题吗,我检查许可证了但是显示有呢
  • ¥15 机器学习预测遇到的目标函数问题