在多个值上加入2个MySQL表

时间:2017-10-30 15:11:41

标签: mysql join subquery

我正在使用this example database,我从两个不同的表中运行以下查询

mysql> select * from salaries where emp_no=10017;
+--------+--------+------------+------------+
| emp_no | salary | from_date  | to_date    |
+--------+--------+------------+------------+
|  10017 |  71380 | 1993-08-03 | 1994-08-03 |
|  10017 |  75538 | 1994-08-03 | 1995-08-03 |
|  10017 |  79510 | 1995-08-03 | 1996-08-02 |
|  10017 |  82163 | 1996-08-02 | 1997-08-02 |
|  10017 |  86157 | 1997-08-02 | 1998-08-02 |
|  10017 |  89619 | 1998-08-02 | 1999-08-02 |
|  10017 |  91985 | 1999-08-02 | 2000-08-01 |
|  10017 |  96122 | 2000-08-01 | 2001-08-01 |
|  10017 |  98522 | 2001-08-01 | 2002-08-01 |
|  10017 |  99651 | 2002-08-01 | 9999-01-01 |
+--------+--------+------------+------------+
10 rows in set (0.01 sec)

mysql> select * from titles where emp_no=10017;
+--------+--------------+------------+------------+
| emp_no | title        | from_date  | to_date    |
+--------+--------------+------------+------------+
|  10017 | Senior Staff | 2000-08-03 | 9999-01-01 |
|  10017 | Staff        | 1993-08-03 | 2000-08-03 |
+--------+--------------+------------+------------+
2 rows in set (0.01 sec)

现在我想要的是加入这两个表,以便我得到下表

emp_no  title           salary  years
10017   Staff           71380   1993
10017   Staff           75538   1994
10017   Staff           79510   1995
10017   Staff           82163   1996
10017   Staff           86157   1997
10017   Staff           89619   1998
10017   Staff           91985   1999
10017   Staff           96122   2000
10017   Staff           98522   2001
10017   Senior Staff    99651   2002

但是我尝试加入这两个表我得到的重复值如下所示是合乎逻辑的,因为表2中的2个值与表1中的10个值相关。我还尝试使用from_date连接多个列但是你可以看到日期略有不同,所以我不能这样做。

mysql> select salaries.emp_no, titles.title, salaries.salary, salaries.from_date
from titles
inner join salaries
on salaries.emp_no = titles.emp_no
where salaries.emp_no=10017; 

emp_no  title           salary  from_date
10017   Senior Staff    71380   1993-08-03
10017   Senior Staff    75538   1994-08-03
10017   Senior Staff    79510   1995-08-03
10017   Senior Staff    82163   1996-08-02
10017   Senior Staff    86157   1997-08-02
10017   Senior Staff    89619   1998-08-02
10017   Senior Staff    91985   1999-08-02
10017   Senior Staff    96122   2000-08-01
10017   Senior Staff    98522   2001-08-01
10017   Senior Staff    99651   2002-08-01
10017   Staff           71380   1993-08-03
10017   Staff           75538   1994-08-03
10017   Staff           79510   1995-08-03
10017   Staff           82163   1996-08-02
10017   Staff           86157   1997-08-02
10017   Staff           89619   1998-08-02
10017   Staff           91985   1999-08-02
10017   Staff           96122   2000-08-01
10017   Staff           98522   2001-08-01
10017   Staff           99651   2002-08-01

我的问题:如果知道没有其他方式可以链接它们,我如何获得所需的结果?或者有什么我忘记的东西?

1 个答案:

答案 0 :(得分:1)

检查日期是否重叠:

Determine Whether Two Date Ranges Overlap

select salaries.emp_no, titles.title, salaries.salary, salaries.from_date
from titles
inner join salaries
   on salaries.emp_no = titles.emp_no
  AND salaries.from_date <= titles.to_date 
  AND salaries.to_date >= titles.from_date
where salaries.emp_no=10017; 

编辑:可能只是一个包含检查才能完成工作

  AND salaries.from_date >= titles.from_date
  AND salaries.to_date < titles.to_date