0

有两个表:

|idperson|工作负载|活动|idpm|名字|姓|

|idsupportday|日期|fk_person|

和这个 SQL 语句:

SELECT person.idperson, person.firstname, person.lastname, person.workload,
       person.active, person.idpm, COUNT(supportday.idsupportday) AS SupportDays,
       MIN(supportday.date) AS FirstDate, MAX(supportday.date) AS LastDate
FROM   person
INNER JOIN supportday 
ON         person.idperson = supportday.fk_person
WHERE      (person.active = 1)
GROUP BY   person.idperson, person.firstname, person.lastname, person.workload,
           person.active, person.idpm

现在如果supportday表是空的,我也会收到一个空的结果。但我仍然希望所有人都person.active=1在结果中。

我需要改变什么?

感谢您的回答。

4

2 回答 2

5

更改INNER JOINLEFT OUTER JOIN

SELECT person.idperson, person.firstname, person.lastname, person.workload,
       person.active, person.idpm, COUNT(supportday.idsupportday) AS SupportDays,
       MIN(supportday.date) AS FirstDate, MAX(supportday.date) AS LastDate
FROM   person
LEFT OUTER JOIN supportday 
ON         person.idperson = supportday.fk_person
WHERE      (person.active = 1)
GROUP BY   person.idperson, person.firstname, person.lastname, person.workload,
           person.active, person.idpm

仅当两个表中至少有一行与连接条件匹配时,内部连接才返回行。内连接消除了与另一个表中的行不匹配的行。然而,外部连接至少返回 FROM 子句中提到的一个表或视图中的所有行,只要这些行满足任何 WHERE 或 HAVING 搜索条件。所有行都从使用左外连接引用的左表中检索,并且从右外连接引用的右表中的所有行检索。

于 2012-04-26T08:37:25.397 回答
3

更改INNER JOINLEFT OUTER JOIN

于 2012-04-26T08:36:59.917 回答