在我的Access数据库中,我试图显示三个表中的数据:
我想要来自PERS
的所有行以及来自COURSE
和WORKED
的一些匹配字段。 KNO
是所有表格中的公共字段
例如:SELECT [KNO],[Name],[Company],[Location],[State] FROM [Pers]
If the KNo had done CCNA (Course.Qualification)
if he had Worked in Channai or Kolkatta or in Bangalore (Worked.Coyloc)
然后才显示
我用过:
Select
Pers.KNO, Pers.Name, Pers.Company, Pers.Location, Pers.State
from Pers
left join (Course on pers.KNo=Course.KNO)
left join Worked.KNo=Per.KNO
where
Course.Qualification='CCNA'
and (Worked.Coyloc='Bangalore' or Worked.Coyloc='Channai or Worked.Coyloc='Kolkatta')
我在将[Worked]表添加到查询时遇到问题。当我只包括[Pers]和[Course]时,我得到了我期望的结果。
Select
Pers.KNO, Pers.Name, Pers.Company, Pers.Location, Pers.State
from Pers
left join Course
on pers.KNo=Course.KNO
where
Course.Qualification='CCNA'
答案 0 :(得分:3)
如果您在编写查询时遇到问题,有时使用Access中的查询设计器进行模拟会很有帮助,然后查看它生成的SQL。对于您的查询:
切换到SQL View会显示以下内容(为了清晰起见,重新格式化):
SELECT Pers.KNO, Pers.Name, Pers.Company, Pers.Location, Pers.State
FROM
(
Pers
LEFT JOIN
Course
ON Pers.KNO = Course.KNO
)
LEFT JOIN
Worked
ON Pers.KNO = Worked.KNO
WHERE (((Course.Qualification)="CCNA") AND ((Worked.Coyloc)="Bangalore"))
OR (((Course.Qualification)="CCNA") AND ((Worked.Coyloc)="Channai"))
OR (((Course.Qualification)="CCNA") AND ((Worked.Coyloc)="Kolkatta"));
请注意,FROM子句中的括号很重要。访问对此有点挑剔。
其他说明:
您可能已经注意到,WHERE子句可以重新组织为
WHERE Course.Qualification="CCNA"
AND (Worked.Coyloc="Bangalore" OR Worked.Coyloc="Channai" OR Worked.Coyloc="Kolkatta")
此外,由于[Course]和[Worked]表中的列都有特定的WHERE条件,因此您实际上不需要使用LEFT JOIN。 INNER JOINs已经足够了。