2014-01-18 211 views
-1

我的項目有幾張圖片存儲在一張表中。選擇語句只選擇一個值

我想只檢索第一個圖像。

我該怎麼做?

這是我的代碼:

SELECT DISTINCT 
    Item.ItemID, Item.Name, Item.Description, Item.Quantity, 
    Item.Condition, Category.Name AS Expr1, Auction.EndDate, Auction.CurrencyValuePost, 
    Image.Image 
FROM 
    Item 
INNER JOIN 
    Category ON Item.CategoryID = Category.CategoryID 
INNER JOIN 
    Image ON Item.ItemID = Image.ItemID 
INNER JOIN 
    Auction ON Item.ItemID = Auction.ItemID 
WHERE   
    (Auction.Status = 'Valid') 
+1

如果我用TOP你可以使用頂部1而不是 –

+0

1,它會給僅在第一記錄我,我希望每一個項目在表中,但只有表中的第一張圖片 – user1784696

+0

作爲第一個圖像您指Image.Image?如果我理解正確,你想選擇只有一列的表 - 圖像? – Marek

回答

0

試試這個

Select * From 
(
SELECT 
    Item.ItemID, 
    Item.Name, 
    Item.Description, 
    Item.Quantity, 
    Item.Condition, 
    Category.Name AS Expr1, 
    Auction.EndDate, 
    Auction.CurrencyValuePost, 
    Image.Image, 
    ROW_NUMBER() OVER(PARTITION BY Item.ItemID ORDER BY Item.ItemID ASC) AS RN 
FROM Item 
    INNER JOIN Category ON Item.CategoryID = Category.CategoryID 
    INNER JOIN Auction ON Item.ItemID = Auction.ItemID 
    LEFT JOIN Image ON Item.ItemID = Image.ItemID 
WHERE Auction.Status = 'Valid' 
) AS T 
Where RN=1 
+0

我已經在這裏添加。請檢查現在 –

+0

這一個是好的謝謝:) – user1784696

+0

@ user1784696標記有用,如果它是幫助你 –

0
SELECT 
    Item.ItemID, 
    Item.Name, 
    Item.Description, 
    Item.Quantity, 
    Item.Condition, 
    Category.Name AS Expr1, 
    Auction.EndDate, 
    Auction.CurrencyValuePost, 
    Image.Image 
FROM Item 
    INNER JOIN Category ON Item.CategoryID = Category.CategoryID 
    INNER JOIN Auction ON Item.ItemID = Auction.ItemID 
    LEFT JOIN Image ON Item.ItemID = Image.ItemID 
WHERE  (Auction.Status = 'Valid') 
+0

不工作:(代碼不好 – user1784696

+1

@ user1784696也許你應該更好地發佈表格結構。 – Marek