左外连接不会在postgres中显示预期结果

时间:2017-09-21 07:30:59

标签: sql postgresql-9.3 outer-join

我有两张表用于存储mont的日期,另一张用于保存员工的出勤详情

在我的情况下,attendance_days表有31条记录 在员工出勤表中,一名员工有29名员工,另一名员工共有8项记录共37项记录 数据如下

Form

`

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

第一张表有31条记录(8月31日)第二张表有37条记录(employee_id 407有29条记录,employee_id有601条记录

我执行了以下查询

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

它只返回37行,但我预计会有62行(员工每行31行)

我无法理解这个问题

1 个答案:

答案 0 :(得分:0)

postgres为attendance_days的每个id找到匹配的行,因此没有理由创建人工命中(看起来像" id,attendance_date,null,null,null,null,null&#34 ;)

您实际上期待的结果如" id,attendance_date,null,null,employee_id,null,null"。

因此,你必须建立一个参赛号为attendance_days.id和employee_id的笛卡尔积(来自"员工表"),它可以为你提供两种组合(62行)。 此笛卡尔积可以与employee_attendance表外连接,以产生所需的结果。