2016-09-13 33 views
0

下面的代碼選擇3級隨機的物品(照片)的價格,並將其傳遞到我的網站作爲一個字符串..從另一個SQL表中選擇的數據添加到字符串文本

SELECT TOP 3 
    thisweeksDate 
    ,'<br/><a href="catalog/images/' 
       + [filename] + 
       '" class="nyroModal" rel="gal" title="' 
       + [price] + 
       '" ><img src="catalog/images/thumbnails/' 
        + [filename] + 
        + '" /></a>' 
        + [price] 
    as strText 
    ,fileID 
    FROM [OCBUser].[tblItems] 
    WHERE thisweeksDate = @thisweeksDate and price <> '' 
    ORDER BY NEWID() 

上述作品完美,但我不知道夠不夠SQL尚未做到以下幾點..

我需要它也選擇

friendlyOrderID from [OCBUser].[tblOrders] 
    where [OCBUser].[tblItems].accountID = [OCBUser].[tblOrders].accountID 

,並與超鏈接p添加到strText的refix所以它可以點擊。因此,這將需要inclide像...

<a href="www.mysite.com/' + [friendlyorderID] + '"

任何想法?

很多謝謝。

+0

你試圖使用JOIN? – OHHO

回答

1

使用JOIN

SELECT TOP 3 thisweeksDate, '<br/><a href="catalog/images/' + [filename] + '" class="nyroModal" rel="gal" title="' + [price] + '" ><img src="catalog/images/thumbnails/' + [filename] + '" /></a>' + [price] as strText, fileID 
    ,'<a href="www.mysite.com/' + [friendlyorderID] + '">link</a>' 

    FROM [OCBUser].[tblItems] 
    JOIN [OCBUser].[tblOrders] on [OCBUser].[tblItems].accountID = [OCBUser].[tblOrders].accountID 
    WHERE thisweeksDate = @thisweeksDate and price <> '' 
    ORDER BY NEWID() 
+0

非常感謝您的回覆..它看起來會工作..我只是從開發人員爲我編寫的代碼學習..沒有使用加入我自己,但我看到如何工作現在謝謝你。然而,我收到以下錯誤,我想因爲thisweeksdate存在於[OCBUser]。[tblOrders]和[OCBUser]。[tblItems] ..我需要它從[OCBUser]中選擇。[tblItems]這是錯誤.. ... 消息209,級別16,狀態1,過程翻譯,行13 不明確的列名'ThisweeksDate'。 消息209,級別16,狀態1,過程Rummage,第8行 不明確的列名稱thisweeksDate。 – Eggybread

+0

我真的很感謝你的幫助。 – Eggybread

+0

沒關係,我懷疑它..我添加了方括號內容:WHERE [OCB]。[tblItems]。[thisweeksDate] = @thisweeksDate and price <>'' – Eggybread

相關問題