sql >> Database >  >> RDS >> Mysql

gegevens ophalen uit mysql-database op basis van verloftypes zoals Examen, Onbetaald, Jaarlijks

Oké, laten we dit proberen en kijken of het werkt.

Dit is het schema waar ik wat mee heb. Het is heel eenvoudig, maar ondersteunt uw gebruiksscenario.

CREATE TABLE employees (
  id int unsigned auto_increment,
  name varchar(255),
  PRIMARY KEY(id)
);

CREATE TABLE leave_type (
  id int unsigned auto_increment,
  name varchar(255),
  PRIMARY KEY(id)
);

CREATE TABLE leave_log (
  id int unsigned auto_increment,
  leave_type_id int unsigned,
  employee_id int unsigned,
  is_full_day int unsigned,
  is_half_day int unsigned,
  PRIMARY KEY(id)
);

Enkele testgegevens ...

INSERT INTO employees VALUES (14, 'Lisa'), (15, 'Homer'), (13, 'Bart');
INSERT INTO leave_type VALUES (1, 'Annual'), (2, 'Unpaid'), (3, 'Exam');
INSERT INTO leave_log VALUES (NULL, 3, 14, 1, 0), (NULL, 1, 14, 1, 0), (NULL, 1, 14, 0, 1), (NULL, 1, 14, 0, 1);
INSERT INTO leave_log VALUES (NULL, 2, 15, 0, 1);
INSERT INTO leave_log VALUES (NULL, 3, 13, 1, 0), (NULL, 1, 13, 1, 0);

Focus niet te veel op kolomnamen en definities, ik heb het schema helemaal niet opgepoetst omdat ik niet genoeg over je app weet om dit te doen.

Zodra het schema is gemaakt en de gegevens erin staan, zou deze vrij eenvoudige query moeten doen wat je wilt.

SELECT e.name, SUM(annual.is_half_day), SUM(unpaid.is_half_day), SUM(exam.is_half_day), 
       SUM(annual.is_full_day), SUM(unpaid.is_full_day), SUM(exam.is_full_day)
  FROM employees e
       LEFT JOIN leave_log annual ON annual.leave_type_id = 1 AND annual.employee_id = e.id
       LEFT JOIN leave_log unpaid ON unpaid.leave_type_id = 2 AND unpaid.employee_id = e.id
       LEFT JOIN leave_log exam ON exam.leave_type_id = 3 AND exam.employee_id = e.id
 GROUP BY e.id

Kijk en zie wat je denkt. Is dit waarschijnlijk een krachtige of zwaarbelaste toepassing?

BEWERKEN

Deze zoekopdracht is ingewikkelder en heeft waarschijnlijk enkele prestatieproblemen, maar is mogelijk nauwkeuriger.

SELECT e.name, e.id,     
       IFNULL(annual_half.total, 0) annual_half,
       IFNULL(unpaid_half.total, 0) unpaid_half,
       IFNULL(exam_half.total, 0) exam_half,
       IFNULL(annual_full.total, 0) annual_full,
       IFNULL(unpaid_full.total, 0) unpaid_full,
       IFNULL(exam_full.total, 0) exam_full
  FROM employees e    
       LEFT JOIN ( SELECT SUM(is_full_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_full_day = 1 AND leave_type_id = 1 GROUP BY 3, 2) annual_full ON annual_full.employee_id = e.id    
       LEFT JOIN ( SELECT SUM(is_full_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_full_day = 1 AND leave_type_id = 2 GROUP BY 3, 2) unpaid_full ON unpaid_full.employee_id = e.id    
       LEFT JOIN ( SELECT SUM(is_full_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_full_day = 1 AND leave_type_id = 3 GROUP BY 3, 2) exam_full ON exam_full.employee_id = e.id    
       LEFT JOIN ( SELECT SUM(is_half_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_half_day = 1 AND leave_type_id = 1 GROUP BY 3, 2) annual_half ON annual_half.employee_id = e.id    
       LEFT JOIN ( SELECT SUM(is_half_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_half_day = 1 AND leave_type_id = 2 GROUP BY 3, 2) unpaid_half ON unpaid_half.employee_id = e.id    
       LEFT JOIN ( SELECT SUM(is_half_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_half_day = 1 AND leave_type_id = 3 GROUP BY 3, 2) exam_half ON exam_half.employee_id = e.id
 GROUP BY 1;



  1. Efficiënte aanpak om de tijdelijke tabel te vullen

  2. Hoe kan ik JSON-arrays in postgresql ontleden?

  3. Sommige gegevens migreren van MySQL naar MSSQL

  4. Een docker-container toevoegen aan een bestaand docker-netwerk