我知道这里的大多数人倾向于不喜欢在这里提出家庭作业问题的人,但我真的在我的数据库系统课上被一个问题难住了。
- 列出所有城市(城市名称和邮政编码,其中至少有一处物业由在'1119 Leighton Ave' 的分公司工作的员工管理。按城市名称的升序排列您的结果。否则,零分。
数据库架构是:
Branch (bNo, street, zipcode)
Staff (sNo, fName, lName, position, sex, dob, salary, bNo)
Property (pNo, street, zipcode, type, room, rent, oNo, sNo, bNo)
Owner (oNo, fName, fName, street, zipcode, phone)
Client (cNo, fName, lName, phone, prefType, maxRent)
Viewing (pNo, cNo, viewDate, cmmt)
Zip (city, state, zipcode)
这是我试图放在一起的查询,非常不成功......
SELECT z.city, p.zipcode
FROM Zip z, Property p
WHERE p.bNo = (SELECT bNo
FROM Branch
WHERE street = '1119 Leighton Ave');
我在连接方面遇到了一些困难。上面的这个陈述甚至没有接近正常工作。我正在翻阅我的书和互联网,试图找到某种帮助。任何帮助将不胜感激。提前致谢。
更新
我目前正在尝试:
SELECT DISTINCT Zip.city, Zip.zipcode AS Zip
FROM Zip
JOIN Property ON Zip.zipcode = Property.zipcode
JOIN Branch ON Zip.zipcode = Branch.zipcode
WHERE Property.bNo = (SELECT bNo
FROM Branch
WHERE Street = '1119 Leighton Ave')
ORDER BY Zip.city;
输出看起来好多了,但我认为它不正确。我现在正在检查它。
更新#2
好的,所以通过运行这个查询:
SELECT DISTINCT zipcode
FROM Property
WHERE bNo = 'B001';
就像 FYI B001 是位于 1119 Leighton Ave 的分行的分行编号一样。无论如何,该命令给了我以下邮政编码:
36205
36251
36264
36206
36277
36272
36265
36203
36201
当我运行上述命令时:
SELECT DISTINCT Zip.city, Zip.zipcode AS Zip
FROM Zip
JOIN Property ON Zip.zipcode = Property.zipcode
JOIN Branch ON Zip.zipcode = Branch.zipcode
WHERE Property.bNo = (SELECT bNo
FROM Branch
WHERE Street = '1119 Leighton Ave')
ORDER BY Zip.city;
这是我的输出:
City Zip
--------------------- -----
ANNISTON 36206
JACKSONVILLE 36265
所以,我相信这个查询有点不对劲。但是你们给了我一个很好的开始,我会继续玩它,我相信我可能会弄明白。非常感谢你们。
我想我已经弄明白了,感谢大家的帮助。
SELECT DISTINCT Zip.city, Zip.zipcode AS Zip
FROM Zip
JOIN Property ON Zip.zipcode = Property.zipcode
JOIN Branch ON Zip.zipcode = Property.zipcode
JOIN Staff ON Staff.bNo = Branch.bNo
WHERE Property.bNo = (SELECT bNo FROM Branch WHERE Street = '1119 Leighton Ave')
ORDER BY Zip.city