0

嘿,我有以下查询:

SELECT UA.Fname, UA.LName, UA.Title, UA.AccDate, UA.Address1, 
  UA.Address2, UA.City, UA.State, UA.Zip, UA.Email, UA.Phone, 
  LabCaseNum, SpecCaseNum, deadlineDate, CaseDetail, 
  AU.Fname as AuthName, AU.Lname, AU.Phone, AU.Email, AU.Role 
FROM userAccount UA 
INNER JOIN userCase UC ON UA.AccNum = UC.Userlink 
INNER JOIN authPerson AU ON UC.CaseNum = AU.UserCase 
WHERE UC.CaseNum = 7903579800;

如果 AU 表中有数据,则效果很好。但是,如果AU表中没有记录,即使 UC 和 UA 表中始终有数据,也不会返回任何记录。

如果没有要收集的数据,我如何格式化上面的查询以忽略AU表?

4

2 回答 2

1

您是否尝试在该表上使用LEFT JOIN :

SELECT UA.Fname, UA.LName, UA.Title, UA.AccDate, UA.Address1, 
  UA.Address2, UA.City, UA.State, UA.Zip, UA.Email, UA.Phone, 
  LabCaseNum, SpecCaseNum, deadlineDate, CaseDetail, 
  AU.Fname as AuthName, AU.Lname, AU.Phone, AU.Email, AU.Role 
FROM userAccount UA 
INNER JOIN userCase UC ON UA.AccNum = UC.Userlink 
LEFT JOIN authPerson AU ON UC.CaseNum = AU.UserCase 
WHERE UC.CaseNum = 7903579800;

这将返回其他表中的所有行,UA如果该表中没有匹配,UC它将返回null这些行。AUUserCase

于 2013-05-08T21:42:52.517 回答
1

在 AU 上尝试左外连接。这将返回第一个表中的所有行,天气 AU 是否有数据。如果没有 AU 数据,则为 AU 字段返回空值

SELECT UA.Fname, UA.LName, UA.Title, UA.AccDate, UA.Address1, 
  UA.Address2, UA.City, UA.State, UA.Zip, UA.Email, UA.Phone, 
  LabCaseNum, SpecCaseNum, deadlineDate, CaseDetail, 
  AU.Fname as AuthName, AU.Lname, AU.Phone, AU.Email, AU.Role 
FROM userAccount UA 
INNER JOIN userCase UC ON UA.AccNum = UC.Userlink 
LEFT OUTER JOIN authPerson AU ON UC.CaseNum = AU.UserCase 
WHERE UC.CaseNum = 7903579800;
于 2013-05-08T21:45:00.360 回答