如何使用联接查询来自多个表的所有记录

时间:2019-06-10 17:43:49

标签: mysql sql

我有我的主表employee_rates_year

tier   skill   rate   year
1      28      110    2019
2      28      101    2019

我的列skill连接到employees表和subcontractors表,为简单起见,这里是tblEmployees的缩写:

EMPLOYEEID   Skill   FullName
1            28      Employee One
2            28      Employee Two

tblSubcontractors

SUBCONTRACTORID   Skill   FullName
1                 28      Sub One
2                 28      Sub Two

我正在尝试为每个员工和分包商显示等级,费率和年份。

我尝试使用不同的联接,但似乎没有任何信息可向我提供每个雇员/分包商的信息。

这是我的查询:

SELECT *
FROM employee_rates_year
left join tblsubcontractors on employee_rates_year.skill = tblsubcontractors.skill
left join tblemployees      on employee_rates_year.skill = tblemployees.skill

问题是我的查询显示如下结果:

tier    skill    rate    year    tblEmployees.FullName    tblSubcontractors.Fullname

1       28       110     2019    Employee One             Sub One
2       28       101     2019    Employee Two             Sub Two

我想得到的是

tier   skill   rate   year   tblEmployees.FullName   tblSubcontractors.FullName

1      28      110    2019   Employee One
1      28      110    2019                           Sub One
2      28      101    2019   Employee Two
2      28      101    2019                           Sub Two

我还尝试使用WHERE尝试区分两个表并选择单个记录。

4 个答案:

答案 0 :(得分:0)

您永远不要使用SELECT *,因为....?

尝试:

SELECT 
  tier,   
  skill,   
  rate,   
  year, 
  tblEmployees.FullName  As Employee
  '' As Subcontractor
FROM employee_rates_year
left join tblsubcontractors on employee_rates_year.skill = tblsubcontractors.skill
union all
SELECT 
  tier,   
  skill,   
  rate,   
  year, 
  '' As Employee
  tblSubcontractors.Fullname As Subcontractor
FROM employee_rates_year
left join tblemployees      on employee_rates_year.skill = tblemployees.skill

答案 1 :(得分:0)

您似乎需要UNION ALL而不是双重加入:

SELECT e.*, t.FullName EmployeesFullName, NULL SubcontractorsFullName
FROM employee_rates_year e
left join tblsubcontractors t on e.skill = t.skill
UNION ALL
SELECT e.*, NULL, t.FullName
FROM employee_rates_year e
left join tblemployees t on e.skill = t.skill
ORDER BY e.tier, e.skill

2的每个查询使用employee_rates_year与其他表1的联接。
最后,将2个结果集与UNION ALL合并。

答案 2 :(得分:0)

使用union all将员工和子联系人聚集在一起,然后使用joingroup by。要获取“垂直列表”中的值,可以使用row_number()

select er.skill, er.rate, er.year,
       max(e_fullname) as e_fullname,
       max(s_fullname) as s_fullname
from ((select e.skill, e.fullname as e_fullname, null as s_fullname,
             row_number() over (partition by e.skill order by e.fullname) as seqnum
       from tblemployees e
      ) union all
      (select s.skill, null as e_fullname, s.fullname as s_fullname,
             row_number() over (partition by s.skill order by s.fullname) as seqnum
       from tblsubcontractors s
      )
     ) es left join
     employee_rates er
     on es.skill = er.skill
group by er.skill, er.rate, er.year, seqnum;

如果您只想将值放在单独的列表中(使用NULL,则不需要row_number()技巧:

select er.skill, er.rate, er.year, e_fullname, s_fullname
from ((select e.skill, e.fullname as e_fullname, null as s_fullname
       from tblemployees e
      ) union all
      (select s.skill, null as e_fullname, s.fullname as s_fullname
       from tblsubcontractors s
      )
     ) es left join
     employee_rates er
     on es.skill = er.skill;

答案 3 :(得分:0)

认为问题(或问题的一部分)是由于“左连接”,它为“左”表中的每一行在结果中创建了一行。由于employee_rates_year只有两行,因此您的查询结果将只有两行。