行业表
+----+--------------+
| ID | IndustryName |
+----+--------------+
| 1 | Auto |
| 2 | Pets |
+----+--------------+
图片表
+----+------------+--------------+
| ID | IndustryId | ImageURL |
+----+------------+--------------+
| 1 | 1 | URL1 |
| 2 | 1 | URL2 |
| 3 | 1 | URL3 |
+----+------------+--------------+
我想获得Select Query的结果如下
+----+------------+------------------+
| ID | IndustryId |ImageURLContains |
+----+------------+------------------+
| 1 | Auto |(URL1,URL2,URL3) |
+----+------------+------------------+
我们可以针对一个IndustryId拥有n个URL。
答案 0 :(得分:0)
尝试使用东西功能
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
关注输出我正在获取
IndstryId IndustryName ImageURL
----------- -------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 Auto URL1 ,URL2 ,URL3