简体   繁体   English

使用JOIN和关联ID从两个表中选择和显示日期

[英]Selecting and displaying date from two tables using JOIN and an associative ID

I have a list of employees in one MySQL table, and a list of assignments in another. 我在一个MySQL表中有一个雇员列表,在另一个MySQL表中有一个工作分配列表。

The employees list contains these 4 fields: 员工列表包含以下4个字段:

+––––––––––––––––––––––––––––––––––––––––––––––
| userid | emp_name | emp_email    | emp_role |
–––––––––––––––––––––––––––––––––––––––––––––––
|   4    |   Jane   | emp@emp.com  |   admin  |
–––––––––––––––––––––––––––––––––––––––––––––––
|   5    |   John   | emp2@emp.com |   guest  |
–––––––––––––––––––––––––––––––––––––––––––––––

and in the assignments table, I have a list of jobs assignments表中,我有一份工作清单

+–––––––––––––––––––––––––––––––––––––––––––––––––––
| userid | job_name |   job_numb   |    due_date   |
––––––––––––––––––––––––––––––––––––––––––––––––––––
|   4    |   Job1   | 012221200000 |   01/21/2017  |
––––––––––––––––––––––––––––––––––––––––––––––––––––
|   5    |   Job2   | 012221200001 |   01/24/2017  |
––––––––––––––––––––––––––––––––––––––––––––––––––––

What I would like is to have all the information from both tables be accessible for data. 我想让两个表中的所有信息都可以访问数据。 What I initially wrote is: 我最初写的是:

<?php
$conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }
$sql = "SELECT * FROM assignments JOIN employees";
$results = mysqli_query($conn, $sql);
    ?>

Then in a loop: 然后循环:

<table>

<?php
    foreach ($results as $result){
        $userid = $result['userid'];
        $emp_name = $result['emp_name'];
        $emp_email = $result['emp_email'];
        $emp_role = $result['emp_role'];
        $job_name = $result['job_name'];
        $job_numb = $result['job_numb'];
        $due_date = $result['due_date'];

    <tr>
        <td><?php echo $emp_name;?></td>
        <td><?php echo $emp_email;?></td>
        <td><?php echo $emp_role;?></td>
        <td><?php echo $job_name;?></td>
        <td><?php echo $job_numb;?></td>
        <td><?php echo $due_date;?></td>
?>
<?php
    }
?>
</table>

Of course, this just outputs everything. 当然,这只是输出所有内容。 I am having trouble understanding how to relate the data from table 1 to table 2 so what I get is the email and name of the employee associated with that job. 我无法理解如何将表1中的数据与表2相关联,所以我得到的是与此工作相关的员工的电子邮件和姓名。 Is there a way to have a conditional in the foreach statement? foreach语句中是否可以有条件?

You are forgetting the JOIN condition (a.userid = e.userid) as far as I can see. 据我所知,您忘记了JOIN条件(a.userid = e.userid)。 Without this condition in the WHERE clause of your SQL statement, you will get a cross product of all tuples from both tables. 如果在SQL语句的WHERE子句中没有此条件,则将从两个表中获得所有元组的叉积

Try the following statement: 请尝试以下语句:

SELECT * FROM assignments a JOIN employees e WHERE a.userid = e.userid;

Note that for the sake of conciseness, I have introduced aliases for the two tables. 注意,为简洁起见,我为两个表引入了别名。

A simple JOIN without any ON condition is like doing the cartesian product of two tables. 没有任何ON条件的简单JOIN就像做两个表的笛卡尔积 You need to use ON as a join condition in your query. 您需要在查询中使用ON作为联接条件。 So your query should be like this: 因此,您的查询应如下所示:

$sql = "SELECT * FROM assignments JOIN employees ON employees.userid = assignments.userid";

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM