【问题标题】:How to join 3 table [closed]如何加入3个表[关闭]
【发布时间】:2013-08-29 15:25:31
【问题描述】:

我想在一页中加入 3 个表格并显示它...

   $id = $_REQUEST["Emp_ID"];
   $test = mysql_query("select * from Employee left join department on employee.dept_id = department.dept_id 
                                               left join leave on employee.leave_id = leave_id where Emp_ID = $id");
   $row = mysql_fetch_assoc($test);

【问题讨论】:

  • 得到错误:mysql_fetch_assoc() 期望参数 1 是资源
  • 在调用mysql_fetch_assoc之前尝试打印mysql_error()的结果。

标签: php mysql sql join


【解决方案1】:

您尝试使用名称employee 加入Employee。如果这是你想要的,你首先需要创建一个别名。

 select * from Employee AS employee 
 left join department on employee.dept_id = department.dept_id 
 left join leave on employee.leave_id = leave.leave_id 
 where employee.Emp_ID = $id

【讨论】:

  • SQL注入和参数化可能值​​得一提...
【解决方案2】:

来这里之前真的应该咨询谷歌。

SELECT t1.col, 
       t1.col,
       t2.col,
       t2.col,
       t3.col,
       t3.col
FROM    t1
    INNER JOIN t2 ON t2.foreignkey = t1.primarykey
    LEFT JOIN t3 on t3.foreignkey = t1.primarykey

如果你不能按照..谷歌。

【讨论】:

    猜你喜欢
    • 2013-06-19
    • 2014-10-14
    • 1970-01-01
    • 2013-05-16
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 2021-08-04
    相关资源
    最近更新 更多