php - 如何在 mysql 中连接 4 个表以获取同一个 html 表上的不同列数据?

标签 php mysql join

我想要的 4 个表的图像:

Image of what I want from 4 tables

如你所见

  • 1.com_name
  • 2.emp_type_name
  • 3.loc_name

3 个不同的表及其主键:com_idemp_type_idloc_id

现在,有一个名为 organization_tbl 的主表,它有 org_idorg_id 在所有表中都是 FK。

我想显示 com_name from company_tbl of org_id=1 emp_type_name from emp_type table where org_id=1 and loc_name from location_tbl where org_id=1 in where clause if I put organization_tbl.org_id=1 那么这应该是必须的输出,但我得到了以下输出重复值。

duplicate values

SELECT emp_type.emp_type_name,location_tbl.loc_name,company_tbl.com_name
from organization_tbl  
left outer join company_tbl 
  on company_tbl.org_id=organization_tbl.org_id 
left outer join location_tbl 
  on  location_tbl.org_id=organization_tbl.org_id
left outer join emp_type
  on emp_type.org_id=organization_tbl.org_id
 GROUP BY   emp_type.emp_type_name,location_tbl.loc_name,company_tbl.com_name,
where organization_tbl.org_id=1

这是我的查询,但无法正常工作。

最佳答案

我觉得你的 WHERE 位置不对:

SELECT 
    employer.emp_type_name,
    location.loc_name,
    company.com_name

FROM organization_tbl organization

LEFT JOIN company_tbl company
  on company.org_id=organization.org_id 

LEFT JOIN location_tbl location
  on  location.org_id=organization.org_id

LEFT JOIN emp_type employer
  on employer.org_id=organization.org_id

WHERE organization.org_id = 1

GROUP BY   
    employer.emp_type_name,
    location.loc_name,
    company.com_name

关于php - 如何在 mysql 中连接 4 个表以获取同一个 html 表上的不同列数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41751281/

相关文章:

php - 如何在 mysqli 准备语句中使用多个内部连接和多个 WHERE 子句?

php - 进入 mysql 数据库后,Café 就变成了 Café

php - 在 PHP 中堆叠多个三元运算符

java - Hibernate 用户模型不保存用户角色和角色

MySQL LEFT JOIN 使用条件运算符(>= 和 <)不返回连接表的空值

sql - sql中从三个表中选择数据

php - 我想要带有 where 条件的 sql 列的最后一个值

php - jQuery Ajax -> 如何避免为一个查询包含每个类

php - 如果没有文件上传,数据库中的文件id应该是: placeholder. gif

php - 如何在 mysql php 中使用 IN OPERATOR 删除所有行