2012-11-27 86 views
0

我期待2代表它們是創建一個視圖頁面:SQL視圖與關係表

tbArticle: 
    articleid 
    name 

tbArticleMedia: 
    articleid 
    mediaid 

我期待創造viewTable這樣的:

viewArticleMedia: 
    ArticleId   (EG vale: 1) 
    ArticleName  (EG vale: this is article) 
    MultimediaId  (EG vale: 1,2,3,4,5) 

請人建議。

回答

0
SELECT 
    a.articleId, a.Name, 
    STUFF(
     (SELECT ',' + CAST(mediaid as VARCHAR(10)) 
      FROM tbArticleMedia 
      WHERE articleid = a.articleid 
      FOR XML PATH ('')) 
      , 1, 1, '') AS MultimediaId 
FROM tbArticle AS a 
    INNER JOIN tbArticleMedia b 
     ON a.articleid = b.articleid 
GROUP BY a.articleId, a.Name 
+0

感謝。這樣可行。非常感謝 – Tun

+0

不客氣:D –