2016-09-13 32 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

相关问题