Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP,如何在另一个表中获得相应的列?_Php_Mysql_Inner Join - Fatal编程技术网

使用PHP,如何在另一个表中获得相应的列?

使用PHP,如何在另一个表中获得相应的列?,php,mysql,inner-join,Php,Mysql,Inner Join,我有一个“联系人”表,如下所示: contact_auto_inc user_id contact_id 1 1 3 2 1 5 3 2 1 4 3 5 5 3 2

我有一个“联系人”表,如下所示:

contact_auto_inc     user_id     contact_id     
      1              1             3
      2              1             5
      3              2             1
      4              3             5
      5              3             2
      6              1             6
 user_id        username
     1          Simon
     2            Bill
     3            Tim
     4          Brendan
     5            Chris
     6            Noel
“用户”表如下所示:

contact_auto_inc     user_id     contact_id     
      1              1             3
      2              1             5
      3              2             1
      4              3             5
      5              3             2
      6              1             6
 user_id        username
     1          Simon
     2            Bill
     3            Tim
     4          Brendan
     5            Chris
     6            Noel
我想要的是打印
联系人ID
的匹配
用户名

例如,对于
user\u id
1,我想打印:

Tim
Chris
Noel
我该怎么做

我可以打印
联系人
表中的
联系人ID
,方法如下:

     $user_id = $_SESSION['user_id'];

//we want to show contact_ids in the contacts table that have the $user_id above. 
     $select_from_contacts_table = "SELECT * FROM contacts WHERE user_id = '$user_id'";

//get the result of the above
     $result=mysqli_query($con,$select_from_contacts_table);

     while($row = mysqli_fetch_assoc($result)) {
     $contact_id=$row['contact_id'];
     echo $contact_id  . "<br>";
 } 
但是如何在
user
表中打印相应的
username
列?我已经尝试了数小时的
内部连接
,但不断出现错误和错误结果,大致如下:

 $select_from_user_table = "SELECT DISTINCT contact_id, username
FROM contacts 
INNER JOIN user
ON contacts.contact_id=user.user_id WHERE user_id = '$user_id'";

//get the result of the above
$result2=mysqli_query($con,$select_from_user_table); 

//show the usernames
while($row = mysqli_fetch_assoc($result2)) {
echo $row['username']  . "<br>";
$select\u from\u user\u table=“选择不同的联系人id、用户名
来自联系人
内部联接用户
在contacts.contact_id=user.user_id,其中user_id='$user_id';
//得到上述结果
$result2=mysqli\u查询($con,$select\u from\u user\u table);
//显示用户名
而($row=mysqli\u fetch\u assoc($result2)){
echo$row['username']。“
”;
}


感谢您的帮助。

您可以在while循环中查询用户名,如下所示:

  while($row = mysqli_fetch_assoc($result)) {
     $contact_id=$row['contact_id'];

     $query = "SELECT username FROM user WHERE user_id = '".$contact_id."' ";
     $result = mysqli_query($con,$query);
     $row = mysqli_fetch_assoc($result)
     echo $row['username'];

}

您可以在while循环中查询用户名,如下所示:

  while($row = mysqli_fetch_assoc($result)) {
     $contact_id=$row['contact_id'];

     $query = "SELECT username FROM user WHERE user_id = '".$contact_id."' ";
     $result = mysqli_query($con,$query);
     $row = mysqli_fetch_assoc($result)
     echo $row['username'];

}

对于
Contact\u id
User\u id
列上的每个连接,您需要使用不同的别名将
Contact
表连接两次回用户表。像

SELECT C.contact_id, U1.username
FROM `contacts` C
INNER JOIN `user` U  ON C.user_id = U.user_id 
INNER JOIN `user` U1 ON C.contact_id = U1.user_id 
WHERE C.user_id = '$user_id'

对于
Contact\u id
User\u id
列上的每个连接,您需要使用不同的别名将
Contact
表连接两次回用户表。像

SELECT C.contact_id, U1.username
FROM `contacts` C
INNER JOIN `user` U  ON C.user_id = U.user_id 
INNER JOIN `user` U1 ON C.contact_id = U1.user_id 
WHERE C.user_id = '$user_id'

你的问题是错误的。您需要执行双重
JOIN
like

SELECT DISTINCT contact_id, username
FROM contacts c
INNER JOIN user u ON c.user_id = u.user_id
INNER JOIN user u1 ON u1.user_id = c.contact_id
WHERE c.user_id = '$user_id'

你的问题是错误的。您需要执行双重
JOIN
like

SELECT DISTINCT contact_id, username
FROM contacts c
INNER JOIN user u ON c.user_id = u.user_id
INNER JOIN user u1 ON u1.user_id = c.contact_id
WHERE c.user_id = '$user_id'
这应该行得通

SELECT contacts.contact_id, user.username
FROM contacts
    INNER JOIN user ON user.user_id = contacts.contact_id
WHERE contacts.user_id = 1
当然,在php代码中使用“$user_id”而不是1

编辑:好的,我没有在你的查询中输入联系人id。但是,如果您不需要与联系人id相关的结果,但需要不同的用户,则添加不同的但删除联系人id,如下所示

解决方案仍然是使用带有表名的user_id,因为它存在于两个表中

SELECT DISTINCT user.username
FROM contacts
    INNER JOIN user ON user.user_id = contacts.contact_id
WHERE contacts.user_id = 1
这应该行得通

SELECT contacts.contact_id, user.username
FROM contacts
    INNER JOIN user ON user.user_id = contacts.contact_id
WHERE contacts.user_id = 1
当然,在php代码中使用“$user_id”而不是1

编辑:好的,我没有在你的查询中输入联系人id。但是,如果您不需要与联系人id相关的结果,但需要不同的用户,则添加不同的但删除联系人id,如下所示

解决方案仍然是使用带有表名的user_id,因为它存在于两个表中

SELECT DISTINCT user.username
FROM contacts
    INNER JOIN user ON user.user_id = contacts.contact_id
WHERE contacts.user_id = 1

我刚刚用内部连接编写了正确的SQL,并为您发布了文章,但我注意到它与您的SQL完全相同,只是不一样——这不会有任何区别。奇怪,它失败了。啊!user_id=末尾的零件。它应该是contacts.user_id。我刚刚用internal JOIN编写了正确的SQL,并为您发布了帖子,但我注意到它与您的完全相同,除了DISTINCT-这不会有任何区别。奇怪,它失败了。啊!user_id=末尾的零件。应该是contacts.user\u id.Brilliant!感谢您的帮助,即使没有明显的问题,现在也能完美地工作。现在对我来说这也很有意义,当然必须像contacts一样将表名放在第一位。contact_id。也感谢下面的所有答案。很高兴听到它有帮助。太棒了!感谢您的帮助,即使没有明显的问题,现在也能完美地工作。现在对我来说这也很有意义,当然必须像contacts.contact_id一样将表名放在第一位。也感谢下面的所有答案。很高兴听到这有帮助。