简体   繁体   English

尝试通过使用另一个表中的id值将两个表连接在一起

[英]Trying to connect two tables together by using id value from one table on the other

So i'm pretty new to sql and i'm trying to figure out how to connect two tables together. 因此,我对sql还是很陌生,我正在尝试弄清楚如何将两个表连接在一起。

I have a table named customers and a table named pets and i want to assign the pets to specific customers. 我有一个名为客户的表和一个名为宠物的表,我想将宠物分配给特定的客户。 I am able to assign them a customer value but only as the id, i can't figure out how to take that id and change it to say, a customer name when i reference it back in a table that displays my data. 我能够为他们分配一个客户值,但只能将其作为ID,当我在显示我的数据的表中引用它时,我不知道如何获取该ID并将其更改为客户名称。

so for example in my customer table the customer id = 10; customerName = "John Smith"; 例如,在我的客户表中, customer id = 10; customerName = "John Smith"; customer id = 10; customerName = "John Smith";

then i have the pets table petId = 16; petName = Alfredo; customerId = 10; 然后我有宠物表petId = 16; petName = Alfredo; customerId = 10; petId = 16; petName = Alfredo; customerId = 10;

Is there a way to reference that customerID = 10 back to the customer table from the pets table so I can pull the name of the customer instead of the id? 有没有一种方法可以将peters表中的customerID = 10引用回到客户表,以便我可以提取客户名称而不是id?

this is my code to display the table that list the pets query, where $row['customer'] I want to show the customer name, not the id. 这是我的代码,用于显示列出宠物查询的表, where $row['customer']我要显示客户名称,而不是ID。

Thanks 谢谢

        <?php 
      $sql = "SELECT * from pets ORDER BY petName ASC";

      echo "<table class='tableInfo' cellpadding='8'>";
      echo "<tr><th>Pet Name</th><th>Owner</th><th colspan='2'>Action</th></tr>";
      $result = mysqli_query($con, $sql);

      while($row = mysqli_fetch_array($result)){    
          echo "<tr>";    
          echo '<td>' . $row['petName'] .'</td>';    
          echo '<td>' . $row['customerId'] .'</td>'; 
          echo '<td><a href="editPets.php?id=' . $row['id'] . '">Edit</a></td>';    
          echo '<td><a href="deletePets.php?id=' . $row['id'] . '">Delete</a></td>';    
          echo "</tr>";    
    }
        echo "</table>";     
    ?>

Yes hi there, you can definitely do that with an inner join: 是的,您好,您可以使用内部联接来做到这一点:

select * from pets
join customers on pets.customerId = customers.customerId
order by petName

It sounds the query may be returning an error. 听起来查询可能返回错误。 Perhaps print the error with: 也许打印错误与:

$res = mysqli_query($con, $sql) or die ('Query failed: ' . mysqli_error($con));

while ($row = mysqli_fetch_assoc($res)) {
    // Do something with row
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从两个表中插入一个表中的值等于另一个表中的值的值? - how to insert values from two tables with value in one table is equal to value in the other? 使用Codeigniter将表中的一行与MySQL中其他两个表的多行联接 - Join one row from a table with multiple rows of two other tables in MySQL using Codeigniter 一次从两个表中检索数据,一个表引用另一个表 - Retrieving data from two tables at once with one table referencing the other PHP Codeigniter MySQL 查询 - 将 4 个表连接在一起,其中 3 个表使用每个表中的一列分组 - PHP Codeigniter MySQL query - join 4 tables together, with 3 tables using group by one column from each table mysql-将一个表与另外两个表联接 - mysql - join one table with two other tables MYSQL:在一张表中搜索用户ID以在其他2张表中搜索数据,然后显示所有3张表中的数据 - MYSQL: Search for User ID in one table to search for data in 2 other tables, then show data from all 3 tables 在其他表中使用关系表的ID - Using relationship table's ID in other tables PHP mysql如何连接两个表来从一个表链接名称和从其他表发布 - PHP mysql how to join two tables to link name from one table and post from other table 使用其他两个表更新表 - Update table using other two tables 尝试使用另一个表中的值搜索一个表 - Trying to search one table using the value from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM