2012-05-31 144 views
0

我的SQL查詢正在吐出3000個查詢時它應該吐出20,我正在使用Oracle。Oracle查詢鏈接查詢

下面是表:

Item (itemNumber, itemName, itemDescription, itemValue, itemLocation, 
categoryID, sellerUsername) 

Auction (auctionNumber, currency, startDateTime, endDateTime, shippingTerms, 
startBidAmount, reserveAmount, bidIncrementAmount, noOfItems, itemSold, 
itemNumber feedbackDateAndTime, rating, comments, paymentDate, paymentid) 

Bid (bidderUsername, auctionNumber, bidDateTime,bidAmount) 

和我的查詢

SELECT 
    i.itemname, 
    i.itemdescription, 
    i.itemvalue, 
    CASE 
     WHEN i.itemnumber=a.itemnumber and a.itemSold='y' THEN 'Sold' 
     WHEN a.auctionnumber != b.auctionnumber and TO_CHAR(sysdate,'DD-MON-YY')>endDateTime THEN 'No Bids on that closed auction' 
     WHEN TO_CHAR(sysdate,'DD-MON-YY')<a.endDatetime and a.auctionnumber=b.auctionnumber 
       and reserveamount>(
       SELECT b.bidAmount 
       FROM dbf12.bid b, dbf12.auction a    
       WHERE a.auctionnumber=b.auctionnumber 
       GROUP BY b.bidAmount 
       HAVING b.bidAmount= max(b.bidAmount)) THEN 'No Bids that meets the reserve' 
     ELSE 'Auction Still Open' 
    END 
FROM 
    dbf12.item i, dbf12.auction a, dbf12.bid b; 

回答

2

看起來你忘了dbf12.item,dbf12.auction和dbf12.bid之間的連接標準。這使得它基本上是三個表的交叉產品,將所有其他行中的每行連接到每行。

嘗試是這樣的:

SELECT 
    i.itemname, 
    i.itemdescription, 
    i.itemvalue, 
    CASE 
     WHEN i.itemnumber=a.itemnumber and a.itemSold='y' THEN 'Sold' 
     WHEN a.auctionnumber != b.auctionnumber and TO_CHAR(sysdate,'DD-MON-YY')>endDateTime THEN 'No Bids on that closed auction' 
     WHEN TO_CHAR(sysdate,'DD-MON-YY')<a.endDatetime and a.auctionnumber=b.auctionnumber 
       and reserveamount>(
       SELECT b.bidAmount 
       FROM dbf12.bid b, dbf12.auction a    
       WHERE a.auctionnumber=b.auctionnumber 
       GROUP BY b.bidAmount 
       HAVING b.bidAmount= max(b.bidAmount)) THEN 'No Bids that meets the reserve' 
     ELSE 'Auction Still Open' 
    END 
FROM 
    dbf12.item i, dbf12.auction a, dbf12.bid b 
    WHERE i.itemnumber = a.itemnumber and b.actionnumber = a.auctionnumber 

你也可以這樣說:

SELECT 
    i.itemname, 
    i.itemdescription, 
    i.itemvalue, 
    CASE 
     WHEN i.itemnumber=a.itemnumber and a.itemSold='y' THEN 'Sold' 
     WHEN a.auctionnumber != b.auctionnumber and TO_CHAR(sysdate,'DD-MON-YY')>endDateTime THEN 'No Bids on that closed auction' 
     WHEN TO_CHAR(sysdate,'DD-MON-YY')<a.endDatetime and a.auctionnumber=b.auctionnumber 
       and reserveamount>(
       SELECT b.bidAmount 
       FROM dbf12.bid b, dbf12.auction a    
       WHERE a.auctionnumber=b.auctionnumber 
       GROUP BY b.bidAmount 
       HAVING b.bidAmount= max(b.bidAmount)) THEN 'No Bids that meets the reserve' 
     ELSE 'Auction Still Open' 
    END 
    from db12.item i 
     inner join dbf12.auction a on a.itemnumber = i.itemnumber 
     inner join dbf12.bid b on b.auctionnumber = a.auctionnumber 
+0

我這裏想補充的WHERE語句? – user1393064

+0

where子句將在查詢的最後位於from子句 –

+0

之後,其中語句拋出3000+行,內部連接拋出0行。它應該排出20行 – user1393064