MySql join语句返回重复数据

时间:2015-11-15 21:00:36

标签: mysql join

我有一个完全规范化的数据库,并通过mySql运行查询。所需的查询之一是返回所需的材料以及与作业相关的任何材料。我需要将以下2个查询的结果合并到一个select语句中(每个都在一个单独的行中,理想情况下使用连接)。这在下面有更好的解释:

SELECT m.Material_Name
FROM project_materials pm,materials m
WHERE pm.Fitting_ID =  m.Fitting_ID
AND pm.Project_ID = '2';

这给了我:

Timber
Bricks

而且:

SELECT sjt.Job_Name
FROM project_staff ps, staff_job_type sjt
WHERE ps.Staff_job_ID = sjt.Staff_job_ID
AND ps.Project_ID = '2';
;

这给了我:

Brick Layer
Project manager

我尝试将组合组合在一起:

SELECT DISTINCT m.Material_Name, sjt.Job_Name
FROM project_materials pm, materials m, project_staff ps, staff_job_type sjt, projects p
WHERE p.Project_ID = ps.Project_ID
AND p.Project_ID = pm.Project_ID
AND pm.Fitting_ID =  m.Fitting_ID
AND ps.Staff_job_ID = sjt.Staff_job_ID
AND pm.Project_ID = '2';

由于我获得了重复的价值,因此完全没有效果。 I.E.数据库的回复如下:

Timber, Brick Layer
Timber, Project manager
Bricks, Brick Layer
Bricks, Project manager

这导致回复随着数据的增长而不断恶化!

我也尝试过使用JOIN但得到了相同的结果。这是我尝试过的代码:

SELECT Material_Name, Job_Name
FROM projects INNER JOIN project_staff ON projects.Project_ID = project_staff.Project_ID
              INNER JOIN project_materials ON project_materials.Project_ID = projects.Project_ID
              INNER JOIN materials ON materials.Fitting_ID = project_materials.Fitting_ID
              INNER JOIN staff_job_type ON staff_job_type.Staff_job_ID = project_staff.Staff_job_ID
WHERE project_materials.Project_ID = '2'
;

在联接前面的内部,左侧,右侧或没有文字没有任何区别。 在此先感谢并为长篇文章感到抱歉

修改 我想要这样的东西:

SELECT m.Material_Name
FROM project_materials pm,materials m
WHERE pm.Fitting_ID =  m.Fitting_ID
AND pm.Project_ID = '2'
UNION
SELECT sjt.Job_Name
FROM project_staff ps, staff_job_type sjt
WHERE ps.Staff_job_ID = sjt.Staff_job_ID
AND ps.Project_ID = '2';
;

但不是联盟,我想把它放在一个单独的专栏中

1 个答案:

答案 0 :(得分:0)

你尝试过这个:

select m.Material_Name, s.JobName
    from project_materials pm 
    join materials m on pm.Fitting_ID = m.Fitting_ID 
        and pm.Project_ID = m.Project_ID
    join project_staff ps on pm.Project_ID = ps.Project_ID
    join staff_job_type sjt on ps.Staff_job_ID = sjt.Staff_job_ID
where pm.Project_ID = '2'

上述查询无法使用,因为材料没有Project_ID。即使列在材料中可用,结果仍然是双倍,而不是预期的。请忽略它。

我认为我们需要一个与材料上的记录相关联的列,以记录在staff_job_type上,就像外键一样。如果没有这一专栏,我们将最终建立全面的关系。单独的Project_ID是不够的,因为它不是材料和staff_job_type的唯一。列staff_job_id将staff_job_type与project_staff相关联,列Fitting_ID将材料与project_material相关联。没有列将材料与staff_job_type相关联。

material_name job_name        sjt.staff_job_id ps.staff_job_id m.fitting_id pm.fitting_id  
------------- --------------- ---------------- --------------- ------------ ------------- 
Timber        Brick Layer     1                1               1            1           
Bricks        Brick Layer     1                1               2            2           
Timber        Project Manager 2                2               1            1           
Bricks        Project Manager 2                2               2            2           

我建议你创建一个表来将材料与staff_job_type相关联/映射。表应该是这样的:

create table material_job (
  fitting_id int,
  staff_job_id int
);

该表包含材料和staff_job_type关系:

fitting_id  staff_job_id 
----------- ------------ 
1           1            
2           2            

假设材料如下:

fitting_id  material_name                                      
----------- -------------------------------------------------- 
1           Timber                                             
2           Bricks                                             

和staff_job_type就像

staff_job_id job_name                                           
------------ -------------------------------------------------- 
1            Brick Layer                                        
2            Project Manager                                    

project_materials喜欢

project_id  fitting_id  amount      
----------- ----------- ----------- 
2           1           500         
2           2           600         

project_staff like

project_id  staff_job_id amount      
----------- ------------ ----------- 
2           1            100         
2           2            200         

我们可以使用查询

select m.material_name, sjt.job_name from material_job mj
    join materials m on mj.fitting_id = m.fitting_id
    join staff_job_type sjt on mj.staff_job_id = sjt.staff_job_id;

结果

material_name job_name                                           
------------- ---------------
Timber        Brick Layer                                        
Bricks        Project Manager                                    

如果你还需要project_staff和project_material记录,我们可以使用sql:

select m.material_name, sjt.job_name from material_job mj
    join materials m on mj.fitting_id = m.fitting_id
    join staff_job_type sjt on mj.staff_job_id = sjt.staff_job_id
    join project_materials pm on m.fitting_id = pm.fitting_id
    join project_staff ps on sjt.staff_job_id = ps.staff_job_id;
所得

material_name job_name        pm_amount ps_amount      
------------- --------------- --------- ---------
Timber        Brick Layer     500       100                                                           
Bricks        Project Manager 600       200