简体   繁体   中英

SQL select from 3 tables and merge column names

I am trying to get data from 3 tables in MySQL and change/merge their column names. Right now when I set the column names using AS they are coming in as duplicates.

people Table:

id    applicant_id    employee_id
---------------------------------
1     3               6          
2     4               10         
3     12              30            

applicants Table:

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

employees Table:

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

All I have is employee_id . I want to make sure that I am getting ALL notes from both employee and applicant, and I want them to be merged into the same column instead of having duplicate columns with NULL values. I want to return results like below:

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

Where I am at now is:

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    

Is there a way to get this accomplished using only SQL? Or will I have to run separate queries to get the applicant id with the employee id?

You need to use 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

The easiest way is to replicate what you asked for is to use UNION. You can use employee_id = 10 from the employees table for that part. In the case of the applicant you can use a subquery where the applicant_id is extracted from the people table where 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)

One advantage of using JOIN is that the query can be converted into a derived table allowing a single place to indicate the employee_id at the end of the query (or limit the list by applicant_id or people.id with slight modifications). Also, the table could include the people.id to ensure the right person is shown in the query. For instance:

  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)

The statements to create the three tables is below.

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM