MySQL从两个表中选择联接

时间:2019-04-02 04:38:50

标签: php mysql sql

我有两个表“ employees”和“ depends”,

    Employees
  |employee_No| Employee_name  |
  |1558       | Bean           |
  |1557       | Juliet         |
  |1556       | Zeke           |

    Dependents

  |employee_No| dependent_name | relationship|
  |1558       | Kelvin         | Son         |
  |1558       | Mary           | Daughter    |
  |1556       | Janet          | Spouse      |

有没有办法我可以在一个MySQL语句中获取此数据并使用php显示,即循环雇员和该雇员下的家属,然后转移到下一个雇员。

当前php代码为

$employees = select_all_employees()
foreach ($employees as $covered){
  echo $covered['Employee_name'].'<br/>';
$get_dependent = $select_dependent($covered["employee_No "]);
 if($get_dependent != 0){
     foreach($get_dependent as $details){
      echo $details['dependent_name '].' '.$details['relationship'].'<br/>';
     }
  }
}

当有成千上万的员工和家属时,这会花费太多时间

预期结果

|employee_No| dependent_name | relationship|
--------------------------------------------
|1558       | Bean           | principal   |
|1558       | Kelvin         | Son         |
|1558       | Mary           | Daughter    |
|1557       | Juliet         | principal   |
|1556       | Zeke           | principal   |
|1556       | Janet          | Spouse      |

4 个答案:

答案 0 :(得分:1)

获得所需结果的最简单方法是使用UNION表中的Employees行和JOIN的{​​{1}}行及其{{1 }}。我们将此Employees作为派生表来执行,以便随后可以按Dependents对结果进行排序,并且还可以将UNION首先放置在每个employee_No上。通过这种方式,您的PHP代码将成为所有结果的简单循环。

principal

输出:

employee_No

Demo on dbfiddle

答案 1 :(得分:1)

该查询如何:

SELECT a.*, "principal" as 'relationship' FROM Employees a UNION SELECT b.* FROM Dependents b ORDER BY employee_no DESC

DBFIDDLE here

答案 2 :(得分:-1)

您可以使用联接或映射列。检查两个表中的索引以确保查询速度慢。两个表中的索引都应为employee_No。然后循环查询输出​​。

<?php
    $qryOutput = array();
    $query = "SELECT A.*, B.* FROM Employees A LEFT JOIN Dependents B ON   A.employee_No=B.employee_No" // query

    $qryOutput = execute_Query($query); // Check this syntax. For Execute query


    // Loop through your Query Output
    foreach ($qryOutput as $key => $value) 
    {   echo $value["employee_No"]." ".$value["dependent_name"]." ".$value["relationship"]; }

 ?>

答案 3 :(得分:-2)

您可以使用 INNER JOIN LEFT JOIN 来实现它。

尝试类似这样的方法。尽管我尚未测试代码。

您的表需要使用外键引用进行创建才能正常工作。请参阅下面的示例。我已经测试过了。

create table Employees(employee_No int primary key,Employee_name varchar(100));

create table Dependents(employee_No int primary key,dependent_name varchar(100), relationship varchar(100)
 foreign key (employee_No) references Employees(employee_No));

插入测试

insert into Employees(employee_No,Employee_name) values(1558,'Bean');

insert into Employees(employee_No,Employee_name) values(1557,'Juliet');

insert into Dependents(employee_No,dependent_name,relationship) values(1558,'kevin','son');

代码

<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "your-db"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = 'SELECT Employees.employee_No, Employees.Employee_name, Dependents.employee_No, Dependents.dependent_name, 
Dependents.relationship FROM Employees
LEFT JOIN Dependents ON Employees.employee_No = Dependents.employee_No
ORDER BY Employees.Employee_name';

    $result = mysqli_query($con,$query);
    while($row = mysqli_fetch_assoc($result)){ 
  $empno = $row['employee_No'];
  $empname = $row['Employee_name'];
  $relation = $row['relationship'];
 $dependant_name = $row['dependent_name'];

//you can now echo

echo $dependant_name.' '.$relation.'<br/>';
}
?>