简体   繁体   English

PHP,帮助显示相关表的查询结果

[英]PHP, help displaying query results of related tables

I'm new to working with db's and for practice I created a simple database called "Company", with three tables: employee, department, position. 我不熟悉数据库,实际上,我创建了一个名为“ Company”的简单数据库,其中包含三个表:雇员,部门,职位。 The fields in the employee table are: id, name, dept_id, pos_id and am using foreign keys to relate department and position to employee. 员工表中的字段为:id,名称,dept_id,pos_id,并且正在使用外键将部门和职位与员工相关联。

I'd like to display the information like this: 我想显示以下信息:

Department 1
Directors: Kevin, Jane, Tom
Managers: Joe, Fred, Mary

Deparment 2
Directors: Bill, Elizabeth, Frank
Managers: Jennifer, Brian, Nicole

I used a JOIN statement to return all employees with their related positions and departments, but I can't figure out the correct PHP to display them like the example above. 我使用了JOIN语句返回所有具有相关职位和部门的员工,但是我无法像上面的示例那样找到正确的PHP来显示他们。 Should I have not used JOINS? 我应该没有使用过JOINS吗?

SQL: SQL:

$sql = "SELECT employee.name, position.pos_name, department.dept_name 
        FROM employee
        INNER JOIN position
        ON employee.pos_id = position.id
        INNER JOIN department
        ON employee.dept_id = department.id";

    $result = $dbconn->query($sql);

Any help would be great, 任何帮助都会很棒,

Thanks 谢谢

First of all: Adapt your SQL to sort as needed 首先:根据需要调整SQL以进行排序

    $sql = "SELECT employee.name, position.pos_name, department.dept_name 
            FROM employee
            INNER JOIN position
            ON employee.pos_id = position.id
            INNER JOIN department
            ON employee.dept_id = department.id
            ORDER BY dept_name,pos_name";

    $result = $dbconn->query($sql);

now do a double group change loop: 现在进行双组更改循环:

$dname=false;

while (true) {
  $row=mysql_fetch_row($result);

  //done!
  if (!$row) break;

  if (!($row[2]===$dname)) {
    //Group change #1: We have a new department
    $dname=$row[2];
    echo "<br>\nDepartment: $dname";
    $pname=false;
  }

  if (!($row[1]===$pname)) {
    //Group change #2: We have a new Position
    $pname=$row[1];
    echo "<br>\n$dname: ";
    $separator='';
  }

  echo $separator.$row[0];
  $separator=', ';
}

What is $result? 什么是$ result? Is it a db resource, or does the query() method return an array? 它是数据库资源,还是query()方法返回数组?

Assuming $result is an associative array returned by the query() method, you should be able to iterate through it and use conditionals to grab the information you need. 假设$ result是query()方法返回的关联数组,则您应该能够对其进行迭代并使用条件获取所需的信息。

foreach($result as $row)
{
    switch($row['department.dept_name']) {
        case "department 1"
           if($row['position.pos_name']) == 'Director') {
               $department1['directors'][] = $row['employee.name'];
           } else if($row['position.pos_name'] == 'Manager') {
               $department1['managers'][] = $row['employee.name'];
           }
        break;

        case "department 2"
          //repeat for each department

    }
}

This will give you an associative array for each department, then you can iterate over those and echo out HTML in the format you described. 这将为您提供每个部门的关联数组,然后您可以遍历这些部门并以您描述的格式回显HTML。

If $result is a DB resource and not an array, then you would need to do something like: 如果$ result是数据库资源而不是数组,那么您需要执行以下操作:

while($row = mysql_fetch_assoc($result))
{
    $data[] = $row;
}

and then use $data instead of $result in the foreach statement. 然后在foreach语句中使用$ data代替$ result。

I can't test it and I'm not too sure if the condition should be in the JOIN or in a HAVING after, but try to test this: 我无法测试它,也不太确定条件是在JOIN之后还是在HAVING之后,但是尝试测试一下:

    $sql = "
            SELECT department.dept_name,department.id,
            GROUP_CONCAT(DISTINCT directors.name SEPARATOR ', '),
            GROUP_CONCAT(DISTINCT managers.name SEPARATOR ', ')
            FROM department
                JOIN employee AS directors
                    ON directors.pos_id = x //director position
                    AND directors.dept_id = department.id
                JOIN employee AS managers 
                    ON managers.pos_id = x //managers position
                    AND managers.dept_id = department.id
            GROUP BY department.id"
$result = $dbconn->query($sql);
print_r($result->fetchAll());

EDIT: Sorry, my request was (very) wrong, should be fine now 编辑:对不起,我的要求是(非常)错误,现在应该可以了

while ($row = mysql_fetch_array($result)){
echo $row['name of column here']; // repeat this for all columns.
}

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

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