SQL从3个表中选择并合并列名

时间:2013-09-18 23:24:03

标签: mysql sql

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

最简单的方法是复制你要求的是使用UNION。您可以使用该部分的employees表中的employee_id = 10。对于申请人,您可以使用子查询,其中authors_id是从employee表中提取的,其中employee_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');