0

我知道这里的大多数人倾向于不喜欢在这里提出家庭作业问题的人,但我真的在我的数据库系统课上被一个问题难住了。

  1. 列出所有城市(城市名称和邮政编码,其中至少有一处物业由在'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
4

2 回答 2

1

在 Oracle 中有多种编写 JOIN 查询的方法。更标准的 SQL 方法是执行以下操作:

SELECT t1.colNameA, t1.colNameB, t2.colNameC
FROM t1
  JOIN t1.pid = t2.fkid
WHERE
 t1.colNameA='whatever'

考虑到这一点,您可以尝试以下步骤:

  1. 列出您需要的所有列:

    选择 Zip.city, Zip.zipcode

  2. 列出您需要的所有表,包括它们之间的关系:

    FROM Zip
      JOIN Property ON Zip.zipcode=Property.zipcode
      JOIN Branch ON Zip.zipCode=Branch.zipcode
      JOIN etc
    
  3. 添加 WHERE 条件。

    WHERE Branch.street='1119 LeightonAve'
      AND etc
    
  4. 添加 ORDER BY 子句

    ORDER BY Zip.city
    

这不是完整的答案,但希望能帮助您找到正确的方向。

编辑#2 编辑后,我建议您需要研究关键字“ DISTINCT ”。像这样的东西可能更符合您的要求:

SELECT DISTINCT Zip.city, Zip.zipcode
于 2012-10-01T20:58:31.220 回答
0

尝试这个

SELECT z.city, p.zipcode
FROM Zip z
     JOIN Property p ON z.zipcode = p.zipcode  -- city name and zip code where there is at least one property for rent 
     JOIN Staff s ON p.sNo = p.sNo -- managed by staff 
     JOIN Branch b ON s.bNo = b.bNo AND b.street = '1119 Leighton Ave' -- who works in the branch office at ‘1119 Leighton Ave’
ORDER BY z.city

JOIN(INNER JOIN) 确保参与的两个表在至少一行中具有相同的值,否则不返回任何记录。

于 2012-10-01T20:54:07.840 回答