Select 来自多个 table ON 相同字段值的所有数据
Select all data from multiple table ON same field value
这是我的数据库表:
regular_employee :
+----+---------------+-----------------+-----------+----------------+
| id | employee_name | employee_job | join_date | permanent_date |
+----+---------------+-----------------+-----------+----------------+
| 1 | ZELDA | ACCOUNTANT | 2020/02 | 2020/03 |
| 2 | YUGO | QA | 2020/02 | 2020/04 |
| 3 | XAVIER | GENERAL MANAGER | 2020/02 | 2020/05 |
| 4 | WAVY | FACTORY MANAGER | 2020/01 | 2020/02 |
+----+---------------+-----------------+-----------+----------------+
contract_employee :
+----+---------------+--------------+-----------+
| id | employee_name | employee_job | join_date |
+----+---------------+--------------+-----------+
| 1 | ANTONIO | ACCOUNTANT | 2020/01 |
| 2 | BAGGIO | ENGINEER | 2020/02 |
| 3 | CHARLES | QA | 2020/02 |
| 4 | DAVID | QA | 2020/02 |
+----+---------------+--------------+-----------+
我的目标:
Select 所有 employee_job 于 2020/02 年加入公司并按 employee_job
区分/分组
我尝试过的:
SELECT re.employee_job,ce.employee_job
FROM regular_employee AS re,contract_employee AS ce
WHERE re.join_date = '2020/02' AND ce.join_date = '2020/02'
GROUP BY re.employee_job,ce.employee_job
结果是:
+-----------------+--------------+
| employee_job | employee_job |
+-----------------+--------------+
| ACCOUNTANT | ENGINEER |
| ACCOUNTANT | QA |
| GENERAL MANAGER | ENGINEER |
| GENERAL MANAGER | QA |
| QA | ENGINEER |
| QA | QA |
+-----------------+--------------+
我期待的是:
+-----------------+
| employee_job |
+-----------------+
| ACCOUNTANT |
| ENGINEER |
| GENERAL MANAGER |
| QA |
+-----------------+
如何进行查询?
联合查询在这里可能更有意义:
SELECT employee_job FROM regular_employee WHERE join_date = '2020/02'
UNION
SELECT employee_job FROM contract_employee WHERE join_date = '2020/02';
UNION
默认情况下将删除可能出现在两个表的 one/both 中的重复作业。
这是我的数据库表:
regular_employee :
+----+---------------+-----------------+-----------+----------------+
| id | employee_name | employee_job | join_date | permanent_date |
+----+---------------+-----------------+-----------+----------------+
| 1 | ZELDA | ACCOUNTANT | 2020/02 | 2020/03 |
| 2 | YUGO | QA | 2020/02 | 2020/04 |
| 3 | XAVIER | GENERAL MANAGER | 2020/02 | 2020/05 |
| 4 | WAVY | FACTORY MANAGER | 2020/01 | 2020/02 |
+----+---------------+-----------------+-----------+----------------+
contract_employee :
+----+---------------+--------------+-----------+
| id | employee_name | employee_job | join_date |
+----+---------------+--------------+-----------+
| 1 | ANTONIO | ACCOUNTANT | 2020/01 |
| 2 | BAGGIO | ENGINEER | 2020/02 |
| 3 | CHARLES | QA | 2020/02 |
| 4 | DAVID | QA | 2020/02 |
+----+---------------+--------------+-----------+
我的目标: Select 所有 employee_job 于 2020/02 年加入公司并按 employee_job
区分/分组我尝试过的:
SELECT re.employee_job,ce.employee_job
FROM regular_employee AS re,contract_employee AS ce
WHERE re.join_date = '2020/02' AND ce.join_date = '2020/02'
GROUP BY re.employee_job,ce.employee_job
结果是:
+-----------------+--------------+
| employee_job | employee_job |
+-----------------+--------------+
| ACCOUNTANT | ENGINEER |
| ACCOUNTANT | QA |
| GENERAL MANAGER | ENGINEER |
| GENERAL MANAGER | QA |
| QA | ENGINEER |
| QA | QA |
+-----------------+--------------+
我期待的是:
+-----------------+
| employee_job |
+-----------------+
| ACCOUNTANT |
| ENGINEER |
| GENERAL MANAGER |
| QA |
+-----------------+
如何进行查询?
联合查询在这里可能更有意义:
SELECT employee_job FROM regular_employee WHERE join_date = '2020/02'
UNION
SELECT employee_job FROM contract_employee WHERE join_date = '2020/02';
UNION
默认情况下将删除可能出现在两个表的 one/both 中的重复作业。