[英]how to join three tables and retrieve selected columns from the three tables and in oracle?
我有3張桌子。 電影院,訂票和顧客
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個結果..我想不出該怎么做..請幫助我。
在您的代碼中,您沒有加入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'
我不確定您的預訂表的結構。 我希望有更多列,例如票數等。
您的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'
現在,您正在為所有客戶組合獲取結果。
請參閱以下示例:
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張桌子是:
這個例子工作正常...
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中嘗試使用此方法-內部聯接“嘗試一下” 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;
你好,你可以這樣使用join:
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';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.