2012-03-10 53 views
2

我有3張桌子。 電影院,預訂和客戶如何連接三個表並從三個表和oracle中檢索選定的列?

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(即; CUST_ID),客戶名稱(CUST_NAME),誰沒有在巴黎預訂的所有客戶的位置(從電影表)。

我想是 -

cust_id cust_name location 
10  sam  New York 
11  adrian London 
14  tom  London 

我嘗試了很多.... 我的代碼一個是---

SELECT customer.cust_id,customer.cust_name, 
cinema.location as Location FROM booking,cinema,customer 
WHERE booking.c_id=cinema.c_id AND location!='Paris'; 

它給了我15結果.. 我不能想到如何做到這一點.. 請幫我這個。

回答

4

在您的代碼中,您未加入bookingcustomer,這會導致您的問題。我使用顯式連接而不是隱式在這裏。雖然沒有difference,但顯式語法是標準的。

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' 

我不完全確定您的預訂表的結構。我希望幾列,例如數票等

+0

我多糊塗加入,但現在它清除。謝謝洛特。 – user1261319 2012-03-10 18:25:32

3

WHERE statement並不像你想具體。 你需要匹配booking.cust_id to customer.cust_id條件:

爲客戶的所有組合
WHERE booking.c_id = cinema.c_id 
AND booking.cust_id = customer.cust_id 
AND location != 'Paris' 

你現在正在做它的方式,你是歌廳的結果。

0

見下面的例子:

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" 

3個表是:

  1. businessuser
  2. businessimages
  3. featured_cart

這個例子正常工作...

0

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 
0

化合物加入加盟四個表:

使用了4桌

  1. 客戶
  2. 訂單
  3. 訂單明細
  4. 產品

本例工程100%可以在W3Schools的嘗試這個 - 內加入「自己嘗試」 SQL編輯器表是從W3Schools的編輯器拍攝。

QUERY:

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; 
0

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';