2015-12-10 49 views
0

我需要重寫下面的查詢使用「與」,以便它不再使用「與」,但我不知道該怎麼辦它正好。我被告知使用子查詢,但我的嘗試沒有奏效。如何轉換一個SQL「與」,以便它不使用「與」

下面是我需要轉換:

with 
best (cid, title, year, lowest) as (
    select distinct M.cid, O.title, O.year, min(price) 
     from yrb_member M, yrb_purchase P, yrb_offer O 
     where M.club = O.club and 
       M.cid = P.cid and 
       P.title = O.title and P.year = O.year 
     group by M.cid, O.title, O.year 
) 
select C.name, P.title, P.year, qnty, price, lowest 
from yrb_customer C, yrb_purchase P, Best B, yrb_offer O 
where P.cid = B.cid and P.title = B.title and 
     P.year = B.year and P.title = O.title and 
     P.year = O.year and P.club = O.club and 
     C.cid = P.cid and 
     O.price > B.lowest 
order by C.name, P.title, P.year; 

我試圖複製的部分用語句改成where語句,但我收到一個錯誤說「標量全查詢的結果,SELECT INTO語句,或VALUES INTO語句不止一行。「您可以在下面看到我的嘗試(它仍然使用「with」,但我試圖替換一小部分,以查看是否可以在查詢的其餘部分使用相同的技術最終完全刪除「with」語句):

with 
best (cid, title, year, lowest) as (
    select distinct M.cid, O.title, O.year, min(price) 
     from yrb_member M, yrb_purchase P, yrb_offer O 
     where M.club = O.club and 
       M.cid = P.cid and 
       P.title = O.title and P.year = O.year 
     group by M.cid, O.title, O.year 
) 
select distinct C.name, P.title, P.year, qnty, price, lowest 
from yrb_customer C, yrb_purchase P, Best B, yrb_offer O 
where P.cid = 

(select distinct M.cid 
     from yrb_member M, yrb_purchase P, yrb_offer O 
     where M.club = O.club and 
       M.cid = P.cid and 
       P.title = O.title and P.year = O.year 
     ) 

and P.title = B.title and 
     P.year = B.year and P.title = O.title and 
     P.year = O.year and P.club = O.club and 
     C.cid = P.cid and 
     O.price > B.lowest 
order by C.name, P.title, P.year; 
+0

您好!我想你會見我的一位朋友,他的名字是'join'。看看他的個人資料:https://technet.microsoft.com/en-us/library/ms191472(v=sql.105).aspx –

+0

只需在「from」子句中用逗號「no」表示。 –

回答

0

不知道你所使用的數據庫,但可以正常剛從with移動查詢直接進入您的查詢的from條款。像這樣:

select C.name, P.title, P.year, qnty, price, lowest 
from yrb_customer C, yrb_purchase P, 
(
    select distinct M.cid, O.title, O.year, min(price) 
     from yrb_member M, yrb_purchase P, yrb_offer O 
     where M.club = O.club and 
       M.cid = P.cid and 
       P.title = O.title and P.year = O.year 
     group by M.cid, O.title, O.year 
) B, 
yrb_offer O 
where P.cid = B.cid and P.title = B.title and 
     P.year = B.year and P.title = O.title and 
     P.year = O.year and P.club = O.club and 
     C.cid = P.cid and 
     O.price > B.lowest 
order by C.name, P.title, P.year; 
相關問題