跳过列中的一个项目

时间:2010-03-30 15:05:47

标签: sql-server-2005 asp-classic

我创建了一个简单的新闻网站。我将视频和图像存储在IMAGES表中。添加的视频中添加的视频和图像都存储在名为ImagesType的列中。附加到新闻的图像和视频存储在NEWS表的ImagesID列中。当我需要显示新闻的第一张图片时,我的问题就出现了。

即。
图像表:

ImagesID ImagesLgURL                        ImagesType  
1        /FLPM/media/videos/0H7T9C0F.flv    videos     
2     /FLPM/media/images/8R5D7M8O.jpg    images  
3        /FLPM/media/images/0E7Q9Z0C.jpg    images  

NEWS table  
NewsID   ImagesID  NewsTitle  
1        1;2;      Street Chic: Paris            ERROR     
2        3;        Paris Runway                  NO ERROR  

以下代码给出了第二个新闻项的错误,因为列表中存储的第一个ImageID不是图像而是视频。我需要想办法跳过视频项并显示下一张图像。

我希望我有道理。

SQL = "SELECT NEWSID, CATEGORIESID, IMAGESID, NEWSTITLE, NEWSSHORTDESC, NEWSACTIVE, NEWSDATEENTERED"
SQL = SQL & " FROM NEWS N"
SQL = SQL & " WHERE NEWSACTIVE = 1"
SQL = SQL & " ORDER BY NEWSDATEENTERED DESC"
Set objNews = objConn.Execute(SQL)

Do While intLooper1 <= 3 And Not objNews.EOF 

IMAGES =   Split(Left(objNews("IMAGESID"),Len(objNews("IMAGESID"))-1), ";") 

SQL = "SELECT ImagesID, ImagesName, ImagesLgURL, ImagesSmURL, ImagesType"
SQL = SQL & " FROM IMAGES I"
SQL = SQL & " WHERE ImagesID = " & IMAGES(0) & " AND ImagesType = 'images'"
Set objLgImage = objConn.Execute(SQL)

<div>
<a href="?Section=news&SubSection=redirect&NEWSID=<%=objNews("NEWSID")%>">
<img src="<%=objLgImage("ImagesLgURL")%>" alt="<%=objLgImage("ImagesName")%>"  />
</a>
</div>
<%
    objLgImage.Close
    Set objLgImage = Nothing

    intLooper1 = intLooper1 + 1
    objNews.MoveNext 
    Loop
%>

2 个答案:

答案 0 :(得分:1)

您应该在图像表中使用NewsID并在其上放置一个ForeingKey

**NEWS table**
NewsID   NewsTitle  
----------------------------
1        Street Chic: Paris            
2        Paris Runway    

**Images table**
ImageID (PK)  NewsID (FK) ImagesLgURL                        ImagesType  
-------------------------------------------------------------------------
1             1           /FLPM/media/videos/0H7T9C0F.flv    videos     
2             1           /FLPM/media/images/8R5D7M8O.jpg    images  
3             2           /FLPM/media/images/0E7Q9Z0C.jpg    images  

然后您可以轻松选择每条新闻的第一张照片

SQL = "SELECT NEWSID, CATEGORIESID, NEWSTITLE, NEWSSHORTDESC, NEWSACTIVE,"     
SQL = SQL & " NEWSDATEENTERED,"
SQL = SQL & " (SELECT TOP 1 ImageID from Images where Images.NewsID = N.NewsID 
                 AND Images.ImageType = 'Images') as ImageID"
SQL = SQL & " FROM NEWS N"
SQL = SQL & " WHERE NEWSACTIVE = 1"
SQL = SQL & " ORDER BY NEWSDATEENTERED DESC"
Set objNews = objConn.Execute(SQL)

答案 1 :(得分:0)

你真的应该有一个Video表和一个Images表。这将是一个更好的设计,它将以更清洁的解决方案解决您的问题。