我有3個表MySQL的左連接和內連接
僱員=> E_ID(PK),dep_id(FK)
部=> D_ID(PK)
考勤=> ID(PK ),date_of_absence(DATE),e_id_f(FK)
我想知道特定部門的缺席員工人數。
我創建一個名爲MyDates
CREATE TABLE MyDates (mydate date);
表和此過程,以填補與日期MyDates表
CREATE PROCEDURE filldates(dateStart DATE, dateEnd DATE)
BEGIN
WHILE dateStart <= dateEnd DO
INSERT INTO MyDates (mydate) VALUES (dateStart);
SET dateStart = date_add(dateStart, INTERVAL 1 DAY);
END WHILE;
END;
然後我調用的過程來填充MyDates與日期CALL filldates('2017-01-01','2017-03-31');
然後我做了這個選擇語句:
select mydates.mydate, count(attendance.date_of_absence)
from mydates left join attendance on
mydates.mydate = attendance.date_of_absence
where mydates.mydate Between "2017-01-01" AND "2017-01-31"
group by mydates.mydate
這個查詢得到我所需要的,但所有的部門,但對於某個部門的行數不正確,
select mydates.mydate, count(attendance.date_of_absence)
from mydates
left join attendance on mydates.mydate = attendance.date_of_absence
inner join employee on attendance.e_id_f = employee.e_id
where mydates.mydate Between "2017-01-01" AND "2017-01-31" AND employee.dep_id = 4
group by mydates.mydate;
這是一張截圖 IMG
你可以添加你需要的數據和你以樣本形式得到的結果集。 – meet
當然,請看看上面添加的截圖。 –