【问题标题】:How do I get rows with value1 in column1 and value2 in column2 but value2 in column1 does not have value1 in column2?如何获取 column1 中的 value1 和 column2 中的 value2 但 column1 中的 value2 在 column2 中没有 value1 的行?
【发布时间】:2019-06-09 23:52:38
【问题描述】:

我有一张桌子叫contacts:

user_id     contact_id
10294       10295
10294       10293
10293       10294
10295       10296
10296       10294
10297       10296
10298       10295
10295       10294
10293       10297

如何从该表中选择user_id 具有contact_id 但感觉没有得到回报的行 - 我的意思是contact_id 没有user_id 作为contact_id

例如在上述情况下$user_id10295。我想要的行是

   10295       10296

我可以选择带有user_id 的所有行作为10295

        $sql = "SELECT * FROM contacts 
        WHERE user_id = ?";  

        $stmt2 = $con->prepare($sql) or die(mysqli_error($con));
        $stmt2->bind_param('i', $user_id) or die ("MySQLi-stmt binding failed ".$stmt2->error);
        $stmt2->execute() or die ("MySQLi-stmt execute failed ".$stmt2->error);

        $privateReviews = $stmt2->get_result(); 

它会给我:

10295       10296
10295       10294

但我正在寻找的只是:10295 10296

我该怎么做?

【问题讨论】:

  • 这个问题的标题非常混乱。 1095 在哪里,和/或它与 10295 10296 有什么关系?
  • 1095$user_id,这是从我的应用程序发布的。如果您有更好的标题,请提出建议。这是我能想到的最具体的!像Problem with Select statement 这样的东西太笼统了。
  • 一哦九五。而不是一哦二九五

标签: php mysql


【解决方案1】:

你可以这样做:

select *
from contacts
where user_id = ?
  and (user_id, contact_id) not in (
  select contact_id, user_id from contacts
)

【讨论】:

    【解决方案2】:

    我不确定是否有一种聪明有效的方法可以直接在 sql 中执行此操作(当然,请参阅 this answer)。我的方法是进行第二次查询。您的第一个查询会获取查询用户添加的所有联系人。第二个查询将执行相反的操作,并查找添加了我们要查询的用户的所有用户。

    然后比较这两个列表,在这种情况下,您需要第二个列表中不在第一个列表中的所有元素。

    // This line is changed
    $sql = "SELECT contact_id FROM contacts 
    WHERE user_id = ?";
    
    // these lines are unchanged
    $stmt2 = $con->prepare($sql) or die(mysqli_error($con));
    $stmt2->bind_param('i', $user_id) or die ("MySQLi-stmt binding failed ".$stmt2->error);
    $stmt2->execute() or die ("MySQLi-stmt execute failed ".$stmt2->error);
    
    // changed this variable name
    $usersThisUserHasAdded = $stmt2->get_result(); 
    
    //new lines
    $sql = "SELECT user_id FROM contacts 
    WHERE contact_id = ?";
    $stmt3 = $con->prepare($sql) or die(mysqli_error($con));
    $stmt3->bind_param('i', $user_id) or die ("MySQLi-stmt binding failed ".$stmt3->error);
    $stmt3->execute() or die ("MySQLi-stmt execute failed ".$stmt3->error);
    
    $usersWhoHaveAddedThisUser = $stmt3->get_result();
    
    $usersWhoHavedAddedThisUserUnreciprocated = array_diff($usersWhoHaveAddedThisUser, $usersThisUserHasAdded);
    
    // $usersWhoHavedAddedThisUserUnreciprocated should now have all the users we are looking for, if you want this in a different format or as some selection of rows you should be able to format it yourself with the original nuser ID or query the rows again using this list
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      相关资源
      最近更新 更多