我正在尝试从 MySQL 中的 3 个表中获取数据并更改/合并它们的列名。现在,当我使用AS
它们设置列名时,它们将作为重复项出现。
人表:
id applicant_id employee_id
---------------------------------
1 3 6
2 4 10
3 12 30
申请人表:
id applicant_id applicant_note applicant_note_date
-----------------------------------------------------------
1 3 "Was good" 2013-05-01
1 4 "Was so-so" 2013-06-07
2 4 "Was bad" 2013-06-08
3 4 "Was great" 2013-06-10
员工表:
id employee_id employee_note employee_note_date
--------------------------------------------------------
1 10 "Was ok" 2013-07-20
1 10 "Was great" 2013-07-21
2 30 "Was bad" 2013-08-01
3 30 "Was so-so" 2013-08-02
我只有employee_id
. 我想确保我从员工和申请人那里得到所有笔记,并且我希望它们被合并到同一列中,而不是重复列的NULL
值。我想返回如下结果:
note date type
------------------------------------------------
"Was so-so" 2013-06-07 applicant
"Was bad" 2013-06-08 applicant
"Was great" 2013-06-10 applicant
"Was ok" 2013-07-20 employee
"Was great" 2013-07-21 employee
我现在的位置是:
SELECT
applicants.applicant_note AS note,
applicants.applicant_note_date AS date,
employees.employee_note AS note,
employees.employee_note_date AS date
IF(applicants.applicant_id IS NULL, 'employee', 'applicant') as type
FROM
employees
JOIN
people
ON
people.employee_id = employees.employee_id
JOIN
applicants
ON
applicants.applicant_id = people.applicant_id
WHERE
employees.employee_id = 10
有没有办法只使用 SQL 来完成这项工作?或者我是否必须运行单独的查询来获取带有员工 ID 的申请人 ID?