我有兩個表一個用於存儲蒙的日期和其他保持員工左外連接不顯示在Postgres的
在我的條件attendance_days表有31條記錄 在員工考勤表考勤細節預期的結果一個僱員具有29和另一僱員8條共37個記錄 數據如下
create table attendance_days
(id serial primary key not null,
attendance_date date);
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-01');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-02');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-03');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-04');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-05');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-06');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-07');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-08');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-09');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-10');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-11');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-12');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-13');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-14');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-15');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-16');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-17');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-18');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-19');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-20');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-21');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-22');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-23');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-24');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-25');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-26');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-27');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-28');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-29');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-30');
INSERT INTO attendance_days (attendance_date) VALUES ('2017-08-31');`
`
create table employee_attendance
(id serial primary key not null,
attendance_day_id integer ,
employee_id integer,
first_in_time timestamp,
last_out_time timestamp);
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (1, 407, '2017-08-01 09:30:00', '2017-08-01 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (2, 407, '2017-08-02 09:30:00', '2017-08-02 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (3, 407, '2017-08-03 09:30:00', '2017-08-03 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (4, 407, '2017-08-04 09:30:00', '2017-08-04 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (5, 407, '2017-08-05 09:30:00', '2017-08-05 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (6, 407, '2017-08-06 09:30:00', '2017-08-06 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (7, 407, '2017-08-07 09:30:00', '2017-08-07 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (8, 407, '2017-08-08 09:30:00', '2017-08-08 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (9, 407, '2017-08-09 09:30:00', '2017-08-09 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (12, 407, '2017-08-12 09:30:00', '2017-08-12 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (13, 407, '2017-08-13 09:30:00', '2017-08-13 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (14, 407, '2017-08-14 09:30:00', '2017-08-14 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (15, 601, '2017-08-15 10:44:40', '2017-08-15 10:47:36');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (15, 407, '2017-08-15 09:30:00', '2017-08-15 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (16, 407, '2017-08-16 09:30:00', '2017-08-16 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (17, 407, '2017-08-17 09:30:00', '2017-08-17 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (18, 407, '2017-08-18 09:30:00', '2017-08-18 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (18, 601, '2017-08-18 08:12:41', '2017-08-19 07:57:12');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (19, 601, '2017-08-19 07:57:12', '2017-08-19 20:14:01');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (19, 407, '2017-08-19 09:30:00', '2017-08-19 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (20, 407, '2017-08-20 09:30:00', '2017-08-20 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (21, 407, '2017-08-21 09:30:00', '2017-08-21 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (22, 601, '2017-08-22 08:07:00', '2017-08-22 20:43:10');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (22, 407, '2017-08-22 09:30:00', '2017-08-22 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (23, 407, '2017-08-23 09:30:00', '2017-08-23 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (24, 407, '2017-08-24 09:30:00', '2017-08-24 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (24, 601, '2017-08-24 08:06:58', '2017-08-24 20:03:44');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (25, 407, '2017-08-25 09:30:00', '2017-08-25 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (26, 601, '2017-08-26 09:14:24', '2017-08-26 20:21:32');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (26, 407, '2017-08-26 09:30:00', '2017-08-26 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (27, 407, '2017-08-27 09:30:00', '2017-08-27 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (27, 601, '2017-08-27 08:48:09', '2017-08-28 07:55:24');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (28, 407, '2017-08-28 09:30:00', '2017-08-28 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (29, 407, '2017-08-29 09:30:00', '2017-08-29 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (29, 601, '2017-08-29 08:09:44', '2017-08-29 20:04:26');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (30, 407, '2017-08-30 09:30:00', '2017-08-30 20:00:00');
INSERT INTO employee_attendance(attendance_day_id, employee_id, first_in_time, last_out_time) VALUES (31, 407, '2017-08-31 09:30:00', '2017-08-31 20:00:00');`
第一表格有31個記錄第二表(在08月31日)有37條記錄(29個記錄EMPLOYEE_ID 407和8記錄EMPLOYEE_ID 601
我執行以下查詢
SELECT *
FROM attendance_days
LEFT OUTER JOIN employee_attendance
ON employee_attendance.attendance_day_id = attendance_days.id
它只返回37行,但我預計62行(每個員工31行)
我無法理解的問題
你能解釋爲什麼你期望62行? –
在第一個表格中有31行和第二個表格,第二個表格有兩個僱員的記錄 –