在我的SQL 2005數據庫中,我有一個表格,其值存儲爲與其他表格有關係的ID。因此,在我的MyDBO.warranty表中,我正在存儲product_id而不是product_name以節省空間。 product_name存儲在MyDBO.products。SQL:嵌套SELECT在單個字段中具有多個值
當營銷部門拉動的人口統計資料,查詢相關表中的每個ID(下調爲簡潔起見),選擇相應的名稱:
SELECT w1.warranty_id AS "No.",
w1.created AS "Register Date"
w1.full_name AS "Name",
w1.purchase_date AS "Purchased",
(
SELECT p1.product_name
FROM WarrDBO.products p1 WITH(NOLOCK)
WHERE p1.product_id = i1.product_id
) AS "Product Purchased",
i1.accessories
FROM WarrDBO.warranty w1
LEFT OUTER JOIN WarrDBO.warranty_info i1
ON i1.warranty_id = w1.warranty_id
ORDER BY w1.warranty_id ASC
現在,我的問題是,「附件」列在warranty_info表存儲幾個值:
No. Register Date Name Purchased Accessories
---------------------------------------------------------------------
1500 1/1/2008 Smith, John Some Product 5,7,9
1501 1/1/2008 Hancock, John Another 2,3
1502 1/1/2008 Brown, James And Another 2,9
我需要做同樣的事情與我與「產品」做「附件」,並從拉accessory_name MyDBO.accessories表使用accessory_id。我不知道從哪裏開始,因爲首先我需要提取ID,然後以某種方式將多個值連接成一個字符串。所以每行都會有「accessoryname1,accessoryname2,accessoryname3」:
No. Register Date Name Purchased Accessories
---------------------------------------------------------------------
1500 1/1/2008 Smith, John Some Product Case,Bag,Padding
1501 1/1/2008 Hancock, John Another Wrap,Label
1502 1/1/2008 Brown, James And Another Wrap,Padding
我該怎麼做?
編輯>>發佈我的最終代碼:
我創造了這個功能:
CREATE FUNCTION SQL_GTOInc.Split
(
@delimited varchar(50),
@delimiter varchar(1)
) RETURNS @t TABLE
(
-- Id column can be commented out, not required for sql splitting string
id INT identity(1,1), -- I use this column for numbering splitted parts
val INT
)
AS
BEGIN
declare @xml xml
set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'
insert into @t(val)
select
r.value('.','varchar(5)') as item
from @xml.nodes('//root/r') as records(r)
RETURN
END
並據此更新了我的代碼:
SELECT w1.warranty_id,
i1.accessories,
(
CASE
WHEN i1.accessories <> '' AND i1.accessories <> 'NULL' AND LEN(i1.accessories) > 0 THEN
STUFF(
(
SELECT ', ' + a1.accessory
FROM MyDBO.accessories a1
INNER JOIN MyDBO.Split(i1.accessories, ',') a2
ON a1.accessory_id = a2.val
FOR XML PATH('')
), 1, 1, ''
)
ELSE ''
END
) AS "Accessories"
FROM MyDBO.warranty w1
LEFT OUTER JOIN MyDBO.warranty_info i1
ON i1.warranty_id = w1.warranty_id
很好地使用,就(應該)不用說,但你不應該在這樣的warranty_info的acccessories列存儲。您應該有一個包含兩列的連接表Warranty_info_accessories:warranty_id和accessory_id。這將大大簡化您的問題 – Gerrat 2011-04-01 20:50:14
我同意,如果我建造了這個結構,那就是我會做的。不幸的是,數據庫作者正在盡其所能節省空間,這是他使用的方法之一。 – Dexter 2011-04-01 20:59:45
我欣賞所有幫助的人,如果我可以選擇多個答案,我會的。顯然,我只是沒有仔細閱讀,並在我的陳述中遺漏了FOR XML PATH('')',導致了錯誤。我已經應用了所有的建議,但它並不完美。 – Dexter 2011-04-04 17:21:58