嘿,我有以下问题:
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 表?
答案 0 :(得分: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
,它将返回AU
行的UserCase
行在那张桌子里。
答案 1 :(得分:1)
在AU上尝试LEFT OUTER JOIN。这将返回第一张表中所有行,天气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;