【问题标题】:How to print the table using foreign key information?如何使用外键信息打印表格?
【发布时间】:2015-04-21 16:23:27
【问题描述】:

朋友我有3张桌子如下

create table departments
(
  department_id int(20) AUTO_INCREMENT primary key,
  department_name varchar(30) not null
);

create table designations
(
  designation_id int(20) AUTO_INCREMENT primary key,
  designation_name varchar(30) not null
);
create table employees
(
 employee_id int(20) AUTO_INCREMENT primary key,
 employee_name varchar(30) not null,
 department int,
 designation int,
 salary int(20) not null,
 FOREIGN KEY (department) REFERENCES departments(department_id),
 FOREIGN KEY (designation) REFERENCES designations(designation_id),
);

这是我的 3 张桌子。现在我需要查看下面的员工表

员工ID |员工姓名 |指定名称 |部门名称 |员工工资

我如何得到它?以下是我的错误程序

<?php
$database_connection = new mysqli("localhost","root","","employee_application");
if($database_connection->connect_error)
     die ("connection failed".$database_connection->connect_error);

$sql_query = "select employees.employee_name,employees.designation,employees.department,employees.employee_salary,departments.department_id,designations.designation_id
              from employees,departments,designations
              where employees.department = departments.department_id and employees.designation = designations.designation_id";

$result=$database_connection->query($sql_query);
if($result->num_rows > 0){
    echo "<table>";
    while($rows = $result->fetch_assoc()){
        echo "<tr>";
        echo "<td>".$rows["id"]."<td>";
        echo "<td>".$rows["name"]."<td>";
        echo "<td>".$rows["department"]."<td>";
        echo "<td>".$rows["designation"]."<td>";
        echo "<td>".$rows["salary"]."<td>";
        echo "<tr>";
    }
    echo "</table>";
}
else{
echo "Table is empty";
}
?>

【问题讨论】:

  • 你遇到了什么错误?
  • 限制超出,我猜 while 循环语句是错误的

标签: php mysql select foreign-key-relationship


【解决方案1】:

您要求提供employee_salary,但在您的表中,该行称为“salary”。注意!

此外,您应该对 SQL 查询使用 LEFT OUTER JOIN,如下所示:

SELECT e.employee_id, e.employee_name, e.salary, des.designation_name, dep.department_name
FROM employees e
LEFT OUTER JOIN designations des ON e.designation = des.designation_id
LEFT OUTER JOIN departments dep ON e.department = dep.department_id

您也必须更改这些行:

echo "<td>".$rows["employee_id"]."<td>";
echo "<td>".$rows["employee_name"]."<td>";
echo "<td>".$rows["department_name"]."<td>";
echo "<td>".$rows["designation_name"]."<td>";
echo "<td>".$rows["salary"]."<td>";

【讨论】:

  • 我只想打印员工ID、员工姓名、员工工资、职务名称和职务工资。我不想打印名称和部门 ID,所以我该怎么做
  • And 循环内的语句没有执行。是吗?
  • 没问题。不要忘记在 php 变量中始终使用与 MySQL 表中相同的名称,除非您使用别名。
【解决方案2】:

试试这个

select employees.employee_id,
employees.employee_name,
designations.designation_name,
departments.department_name,
employees.salary as employee_salary
  from employees
  left join departments on employees.department = departments.department_id 
  left join designations on employees.designation = designations.designation_id

【讨论】:

  • 代码第5行as的用途是什么
  • 它在mysql服务器上工作但是如何通过while循环获取信息
猜你喜欢
  • 2021-06-06
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 1970-01-01
相关资源
最近更新 更多