SQL多个内部联接数据库

时间:2016-12-08 20:33:50

标签: sql oracle inner-join

早上好,我正在做我的作业,我需要通过订单将customerID字段包含在我的表中。这是我的sql

SELECT 
    northwinds.orders.customerid, northwinds.orderdetails.orderid, 
    northwinds.products.productname, northwinds.orderdetails.unitprice,    
    northwinds.orderdetails.quantity, 
    northwinds.orderdetails.unitprice * northwinds.orderdetails.quantity AS TotalCost
FROM 
    northwinds.orderdetails 
JOIN 
    northwinds.products ON  northwinds.orderdetails.productid = northwinds.products.productid
FROM 
    northwinds.orderdetails 
JOIN 
    northwinds.orders ON  northwinds.orderdetails.orderid = northwinds.orders.orderid

我想我几乎做对了,但它说:

  

第4行的错误:ORA-00933:SQL命令未正确结束

有人能帮助我,我该怎么做?

4 个答案:

答案 0 :(得分:3)

如前所述,您有两个from语句。但是请考虑使用别名和格式化代码。这使得更容易发现问题。您的查询应如下所示:

SELECT o.customerid
     , od.orderid
     , p.productname
     , od.unitprice
     , od.quantity
     , od.unitprice * od.quantity AS TotalCost
FROM northwinds.orderdetails od 
  INNER JOIN northwinds.products p ON od.productid = p.productid
FROM northwinds.orderdetails od 
  INNER JOIN northwinds.orders o ON od.orderid = o.orderid;

使用这种格式更容易注意到双重并将其修改为最终版本和正确版本:

SELECT o.customerid
     , od.orderid
     , p.productname
     , od.unitprice
     , od.quantity
     , od.unitprice * od.quantity AS TotalCost
FROM northwinds.orderdetails od 
  INNER JOIN northwinds.products p ON od.productid = p.productid
  INNER JOIN northwinds.orders o ON od.orderid = o.orderid;

答案 1 :(得分:2)

除了两个FROM之外,还使用表别名使其易于阅读

SELECT o.customerid, 
       od.orderid,
       pr.productname, 
       od.unitprice,    
       od.quantity,  
       od.unitprice * od.quantity AS TotalCost
FROM northwinds.orderdetails as od 
JOIN northwinds.products as pr
  ON od.productid = pr.productid
JOIN northwinds.orders as o
  ON od.orderid = o.orderid

答案 2 :(得分:1)

您的查询有2个FROM子句

SELECT northwinds.orders.customerid, northwinds.orderdetails.orderid,
  northwinds.products.productname, northwinds.orderdetails.unitprice,
  northwinds.orderdetails.quantity, northwinds.orderdetails.unitprice * northwinds.orderdetails.quantity AS TotalCost
FROM northwinds.orderdetails JOIN northwinds.products
    ON  northwinds.orderdetails.productid=northwinds.products.productid
FROM northwinds.orderdetails JOIN northwinds.orders
ON  northwinds.orderdetails.orderid= northwinds.orders.orderid;

修改

删除第二个语句并再次测试。注意没有架构我不知道这个结果查询是否会导致成功:

SELECT northwinds.orders.customerid, northwinds.orderdetails.orderid,
  northwinds.products.productname, northwinds.orderdetails.unitprice,
  northwinds.orderdetails.quantity, northwinds.orderdetails.unitprice * northwinds.orderdetails.quantity AS TotalCost
FROM northwinds.orderdetails 
JOIN northwinds.products
    ON  northwinds.orderdetails.productid=northwinds.products.productid
JOIN northwinds.orders
    ON  northwinds.orderdetails.orderid= northwinds.orders.orderid;

答案 3 :(得分:1)

您的查询中可能没有多个“from”。我改变了一点,并使用一些别名来缩短查询,从我的角度来看,更具可读性。

SELECT ord.customerid, northwinds.orderdetails.orderid, 
        prod.productname, od.unitprice, od.quantity, 
        od.unitprice*od.quantity AS TotalCost
FROM    northwinds.orderdetails od
JOIN    northwinds.products     prod ON  od.productid = prod.productid
JOIN    northwinds.orders       ord  ON  od.orderid   = ord.orderid

希望有所帮助,保重

安德烈亚斯