Oracle关系数据库,查询/连接多个表

时间:2012-10-01 20:39:58

标签: oracle plsql relational-database

我知道这里的大多数人都不喜欢在这里询问家庭作业问题,但我真的很难在我的数据库系统课程中遇到过一个问题。

  1. 列出所有城市(城市名称和邮政编码,其中至少有一个出租物业由在'1119 Leighton Ave'的分公司工作的员工管理。按城市名称的升序排序您的结果。否则,零点。
  2. 数据库架构是:

    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号的分行的分行编号。无论如何,该命令给了我以下邮政编码:

    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
    

2 个答案:

答案 0 :(得分:1)

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

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

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

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

    SELECT 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
    
  5. 这不是完整的答案,但希望能帮助您找到正确的方向。

    编辑#2 在您进行修改后,我建议您需要研究关键字“DISTINCT”。这样的事情可能更符合您的要求:

    SELECT DISTINCT Zip.city, Zip.zipcode
    

答案 1 :(得分: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)确保两个参与的表在至少一行中具有相同的值,否则不返回任何记录。