如何在oracle中联接三个表并从三个表和中检索选定列?

如何在oracle中联接三个表并从三个表和中检索选定列?,oracle,sqlplus,Oracle,Sqlplus,我有三张桌子。 电影院、预订和客户 create table cinema ( c_id int, location varchar(10) ) insert into cinema values(1,'New York'); insert into cinema values(2,'London'); insert into cinema values(3,'Paris'); create table booking ( c_id int, cust_id int ) insert int

我有三张桌子。 电影院、预订和客户

create table cinema
(
c_id int,
location varchar(10)
)
insert into cinema values(1,'New York');
insert into cinema values(2,'London');
insert into cinema values(3,'Paris');

create table booking
(
c_id int,
cust_id int
)

insert into booking values(1,10);
insert into booking values(2,11);
insert into booking values(3,12);
insert into booking values(3,13);
insert into booking values(2,14);

create table customer
(
cust_id int,
cust_name varchar(10)
)

insert into customer values(10,'sam');
insert into customer values(11,'adrian');
insert into customer values(12,'mark');
insert into customer values(13,'jim');
insert into customer values(14,'tom');
我想选择所有未在巴黎预订的客户的客户id(即客户id)、客户名称(客户名称)和位置(从电影院表)

我想要的是--

我试了很多。。。。 我的代码之一是---

它给了我15个结果。。 我想不出怎么做。。
请帮我解决这个问题。

您的
WHERE语句
没有您想要的那么具体。 您需要将booking.cust\u id与customer.cust\u id匹配的条件:

WHERE booking.c_id = cinema.c_id 
AND booking.cust_id = customer.cust_id
AND location != 'Paris'

您现在这样做的方式是,您正在获取所有客户组合的结果。

在您的代码中,您没有加入
booking
customer
,这导致了您的问题。这里我使用显式连接而不是隐式连接。虽然没有,但显式语法是标准的

select cu.cust_id, cu.cust_name, ci.location
  from cinema ci
  join booking b
    on ci.c_id = b.c_id
  join customer cu
    on b.cust_id = cu.cust_id
 where ci.location <> 'Paris'
选择cu.cust\u id、cu.cust\u name、ci.location
来自电影ci
加入预订b
关于ci.c_id=b.c_id
加入客户cu
在b.cust\u id=cu.cust\u id上
地点“巴黎”在哪里

我不能完全确定你们预订桌的结构。我希望还有一些列,例如票证数量等。

请参见以下示例:

Select   
businessuser.UserName,
businessuser.EmailAddress,
businessimages.ImgName, 
featured_cart.FeaturedPlan,featured_cart.StartDate
From featured_cart
Inner Join businessimages on featured_cart.FeaturedProId = businessimages.IdBusinessImages 
Inner Join businessuser  on businessimages.UserId = businessuser.IdBusinessUser
and featured_cart.FeaturedType = "Featured Email"
三个表格分别为:

  • 商业用户
  • 商业形象
  • 特色购物车
  • 此示例正常工作…

    MS SQL Server 2008:

    select cust_id, cust_name, location
    from customer c 
    inner join booking b 
    inner join location l
    on c.cust_id = b.cust_id and b.c_id=l.c_id
    

    复合联接联接四个表:

    使用了4个表格

  • 顾客
  • 命令
  • 订单详情
  • 产品
  • 此示例100%有效您可以在W3schools中尝试此操作-内部联接“try yourself”SQL编辑器表取自W3schools编辑器

    查询:

    Select Customers.CustomerName as Table1_Customer, Orders.EmployeeID as Table2_Employee, OrderDetails.Quantity as Table3_OrderDetails,Products.ProductName as Table4_Products 
    From Customers
    INNER JOIN Orders 
    ON Customers.CustomerID = Orders.CustomerID
    INNER JOIN OrderDetails 
    ON Orders.OrderID = OrderDetails.OrderID 
    INNER JOIN Products
    ON OrderDetails.ProductID = Products.ProductID Order by EmployeeID;
    

    hii您可以像这样使用连接:

    SQL

    select customer.cust_id,customer.cust_name,cinema.location from cinema,customer,booking where cinema.c_id=booking.c_id and booking.cust_id=customer.cust_id and cinema.location not like 'paris';
    

    我对多重连接感到困惑,但现在它被清除了。非常感谢。
    Select Customers.CustomerName as Table1_Customer, Orders.EmployeeID as Table2_Employee, OrderDetails.Quantity as Table3_OrderDetails,Products.ProductName as Table4_Products 
    From Customers
    INNER JOIN Orders 
    ON Customers.CustomerID = Orders.CustomerID
    INNER JOIN OrderDetails 
    ON Orders.OrderID = OrderDetails.OrderID 
    INNER JOIN Products
    ON OrderDetails.ProductID = Products.ProductID Order by EmployeeID;
    
    select customer.cust_id,customer.cust_name,cinema.location from cinema,customer,booking where cinema.c_id=booking.c_id and booking.cust_id=customer.cust_id and cinema.location not like 'paris';