选择外键为null的id

时间:2014-02-25 01:34:44

标签: sql-server

如何从一张桌子中选择一个拍卖ID,在另一张桌子中,拍卖ID为null请问,我有没有办法尝试为null但不工作有人可以帮忙吗?希望能得到答案

SELECT        Auction.AuctionID
FROM            Item 
               INNER JOIN Auction ON Item.ItemID = Auction.ItemID 
               INNER JOIN BID ON Auction.AuctionID = BID.AuctionID
WHERE        (Auction.Status = 'Valid') AND (BID.AuctionID IS NULL)

2 个答案:

答案 0 :(得分:2)

您在拍卖和出价表之间使用内部联接。请改用左连接。

答案 1 :(得分:0)

查找没有出价的拍卖

SELECT        Auction.AuctionID -- other columns 
FROM            Item 
               INNER JOIN Auction ON Item.ItemID = Auction.ItemID 
               Left outer JOIN BID ON Auction.AuctionID = BID.AuctionID
WHERE        (Auction.Status = 'Valid') and bid.auctionid is null
Group by auction.auctionid
Having count(bid.auctionid) = 0