简体   繁体   English

如何使用 PHP 从一对多 sql 关系中检索数据

[英]How to retrieve data from a one to many sql relationship using PHP

I have two tables.我有两张桌子。 One is called "employee" and has a column named "role".一个被称为“员工”,并有一个名为“角色”的列。 I have another table with the name "role" that has two columns in it "role_id" and "name".我有另一个名为“role”的表,其中有两列“role_id”和“name”。

I have linked the two in MySQL with a foreign key (I think,).我已经用外键(我认为)将 MySQL 中的两者链接起来。 by going to relationship view through phpmyadmin and selecting the appropriate settings.通过 phpmyadmin 进入关系视图并选择适当的设置。

Now in PHP I have the following code:现在在 PHP 我有以下代码:

$employees = "SELECT employee_id, first_name, last_name, role 
                 FROM employee 
                 WHERE status = 1"; 
$employee = mysqli_query($link, $employees);

foreach ($employee as $employeeInfo) {
    <?php echo $employeeInfo['role'];?>
}

However, this returns the number I have assigned to the role in the employee's table.但是,这将返回我分配给员工表中角色的号码。 Not the name from the linked table.不是链接表中的名称。

I think it has something to do with having to use the JOIN condition is the PHP code.我认为这与必须使用 JOIN 条件有关,即 PHP 代码。 However, doing some experimentation just gets me blank results.然而,做一些实验只会让我得到空白的结果。

Do you have any tips for outputting the role name from the second table in this case?在这种情况下,您对从第二个表中输出角色名称有什么提示吗?

Thank you谢谢

Use join to get the name of the role.使用 join 获取角色的名称。

$employees = "SELECT a.employee_id, a.first_name, a.last_name, b.name as role 
                 FROM employee a join role b on a.role = b.role_id
                 WHERE a.status = 1"; 
$employee = mysqli_query($link, $employees);

foreach ($employee as $employeeInfo) {
    echo $employeeInfo['role'];
}

Inner Joins should really work...内连接应该真的有效......

SELECT employee_id, first_name, last_name, role.name as role 
    FROM employee 
    INNER JOIN role ON employee.role = role.role_id 
    WHERE status = 1;

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

相关问题 如何使用php和mysql显示数据一对多关系 - How to display data one to many relationship using php and mysql 如何使用PHP在MySQL中过滤数据(一对多关系) - How to filter data in MySQL (One to many relationship) using PHP Laravel 4:如何以一对多关系检索数据? - Laravel 4 : How to retrieve data in a one-to-many relationship? 从 Laravel 中的多对多关系中检索数据 - Retrieve data from many to many relationship in Laravel 我将如何使用 PDO+PHP 从通过多对多关系连接的表中检索数据,使用第一个表中的主键? - How would I use PDO+PHP to retrieve data from a table connected by a many-to-many relationship, using the primary key from the first table? 如何从多对多关系表中获取数据并在一行中打印所有行 PHP - How to get data from many to many relationship table and print all rows in one line PHP 如何在PHP中以一对一关系从两个表中检索数据组合? - How to retrieve data combination from both tables in one to one relationship in PHP? 如何在 Laravel 5.4 中从一对一关系中检索数据 - How to retrieve data from one to one relationship in Laravel 5.4 如何在PHP PDO中选择一对多关系数据 - How to select one to many relationship data in PHP PDO 如何使用PHP从SQL Server存储过程中检索数据 - How to retrieve data from a sql server stored procedure using PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM