2

我正在尝试从 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?

4

2 回答 2

3

你需要使用UNION ALL

SELECT  employee_note note,
        employee_note_date date,
        'employee' type
FROM    people a
        INNER JOIN employees b
            ON a.employee_ID = b.employee_ID
WHERE   a.employee_ID = 10
UNION ALL
SELECT  applicant_note note,
        applicant_note_date date,
        'applicant' type
FROM    people a
        INNER JOIN applicants b
            ON a.applicant_id = b.applicant_id
WHERE   a.employee_ID = 10
于 2013-09-18T23:30:38.190 回答
0

最简单的方法是复制您要求的内容是使用 UNION。您可以使用employees 表中的employee_id = 10 作为该部分。对于申请人,您可以使用子查询,其中申请人 ID 是从员工 ID = 10 的人员表中提取的。

  SELECT  employee_note note, 
      employee_note_date date, 
      'employee' type
  FROM    employees e  
  WHERE   e.employee_id = 10
  UNION  
  SELECT applicant_note note,    
      applicant_note_date date,  
      'applicant' type
  FROM    applicants a
  WHERE   applicant_id = (SELECT applicant_id FROM people WHERE employee_id = 10)

使用 JOIN 的一个优点是可以将查询转换为派生表,从而允许在查询末尾的单个位置指示employee_id(或通过稍微修改的applicant_id 或people.id 限制列表)。此外,该表可以包含 people.id 以确保在查询中显示正确的人。例如:

  SELECT * FROM (
      SELECT  p.id person,
          employee_note note,
          employee_note_date date,
          'employee' type
      FROM    employees e
      JOIN people p on p.employee_id = e.employee_id
      UNION 
      SELECT  p.id person,
          applicant_note note,
          applicant_note_date date,
          'applicant' type
      FROM    applicants a
      JOIN people p on p.applicant_id = a.applicant_id
  ) q
  WHERE q.person in(SELECT id FROM people WHERE employee_id = 10)

创建三个表的语句如下。

CREATE TABLE `people` (`id` int(11) NOT NULL, `applicant_id` int(11) NOT NULL, 
  `employee_id` int(11) NOT NULL, PRIMARY KEY (`id`),
  KEY `applicant_id_index` (`applicant_id`),
  KEY `employee_id_index` (`employee_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `employees` (`employees_id` int(11) NOT NULL AUTO_INCREMENT,
 `id` int(11) NOT NULL, `employee_id` int(11) NOT NULL,
  `employee_note` varchar(12) DEFAULT NULL,
  `employee_note_date` datetime DEFAULT NULL,
  PRIMARY KEY (`employees_id`),
  KEY `employee_id_index` (`employee_id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `applicants` (
  `applicants_id` int(11) NOT NULL AUTO_INCREMENT,
  `id` int(11) NOT NULL,
  `applicant_id` int(11) NOT NULL,
  `applicant_note` varchar(12) DEFAULT NULL,
  `applicant_note_date` datetime DEFAULT NULL,
  PRIMARY KEY (`applicants_id`),
  KEY `applicant_id_index` (`applicant_id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO `employees` VALUES (1,1,10,'Was ok','2013-07-20 00:00:00'),  
 (2,1,10,'Was great','2013-07-21 00:00:00'),(3,2,30,'Was bad','2013-08-01 00:00:00'),  
 (4,3,30,'Was so-so','2013-08-02 00:00:00');
INSERT INTO `people` VALUES (1,3,6),(2,4,10),(3,12,30);
INSERT INTO `applicants` VALUES (1,1,3,'Was good','2013-05-01 00:00:00'),  
 (2,1,4,'Was so-so','2013-06-07 00:00:00'),
 (3,2,4,'Was bad','2013-06-08 00:00:00'),
 (4,3,4,'Was great','2013-06-10 00:00:00');
于 2015-12-16T19:06:26.487 回答