2017-12-06 1 views
0

Industrie TabelleDatenbank schließt sich mit einzelnen Datensatz mit mehreren coulmns

+----+--------------+ 
| ID | IndustryName | 
+----+--------------+ 
| 1 |  Auto  | 
| 2 |  Pets  | 
+----+--------------+ 

Bilder Tabelle

+----+------------+--------------+ 
| ID | IndustryId | ImageURL | 
+----+------------+--------------+ 
| 1 |  1  |  URL1 | 
| 2 |  1  |  URL2 | 
| 3 |  1  |  URL3 | 
+----+------------+--------------+ 

ich Ergebnis Auswahlabfrage wollen als

+----+------------+------------------+ 
| ID | IndustryId |ImageURLContains | 
+----+------------+------------------+ 
| 1 | Auto  |(URL1,URL2,URL3) | 
+----+------------+------------------+ 

folgt und Wir können n Nummer haben von URLs gegen eine IndustryId.

+0

ist IndustryId und ImageURL ein Verbundschlüssel sind? Oder ist IndustryId gleich ImageURL? –

+0

IndustryId der Image-Tabelle bezieht sich auf Industry-Tabellen-ID als Fremdschlüssel –

Antwort

0

Versuchen Sie es mit Stuff Funktion

CREATE TABLE Images_Table(ID INT ,IndustryId INT , IMageURL NVARCHAR(50)) 

INSERT INTO Images_Table 
SELECT 1,1,'URL1'UNION ALL 
SELECT 2,1,'URL2'UNION ALL 
SELECT 3,1,'URL3' 

SELECT 
    ID IndstryId,IndustryName, 
    STUFF(
     (
      SELECT ' ,'+ISNULL(IMageURL,'') 
      FROM Images_Table t2 
      WHERE t2.IndustryId=t1.ID 
      ORDER BY t2.Id 
      FOR XML PATH('') 
     ),1,2,'') ImageURL 
FROM (SELECT ID,ISNULL(IndustryName,'') IndustryName FROM Industry_Table) t1 

Output Im Anschluss Ich erhalte es

IndstryId IndustryName          ImageURL 
----------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
1   Auto            URL1 ,URL2 ,URL3 
Verwandte Themen