我知道的勉強夠用的SQL。我試圖讓MS SQL顯示一個記錄集,應用程序可以直接通過小解析來完成。使用XML PATH連接來自多個WHERE子句的結果
- 客戶的表
- TechContacts的表
- 服務的表
- 網站的表格
- 在TechContact每個條目:
數據庫的結構如下: FK返回給客戶
- 每個條目都是客戶和服務的FK
- 每項服務都可以鏈接到來自多個客戶的一個或多個網站。
所以,如果我想鏈接到一個特定的服務的電子郵件地址的列表,並讓我的成績使用INNER JOIN僅有,它應該是這樣的:
SELECT tc.emailaddress as 'Email Address', s.sitename as 'Affected Site', c.CustomerName as 'Customer Name'
FROM techcontact as tc
INNER JOIN customer as c
on tc.customernumber = c.customernumber
INNER JOIN site as s
on c.customernumber = s.customernumber
INNER JOIN dbo.service as serv
on s.serviceid = serv.serviceid
INNER JOIN sitetype as st
on s.sitetype = st.SiteTypeID
where serv.servicename = 'Service 1'
and st.SiteTypeID = 1
and s.enabled = 1
order by s.SiteName asc
會給我要每個電子郵件地址中的一行:
Email Address Site Affected Customer Name
[email protected] Site A 1
[email protected] Site B 2
[email protected] Site B 2
[email protected] Site B 2
[email protected] Site C 3
[email protected] Site C 3
[email protected] Site D 3
[email protected] Site D 3
經過一番搜索,我發現對於使用XML路徑來連接的結果類似的問題的答案。這是我用來獲取一個有效的解決方案查詢:
Select distinct c.customernumber,
substring(
(
Select ';'+tc.emailaddress AS [text()]
From dbo.techcontact tc
Where tc.customernumber = c.customernumber
For XML PATH ('')
), 2, 1000) [Emails],
substring(
(
Select distinct ', '+s.SiteName AS [text()]
From dbo.Site s
inner join service
on s.serviceid = serv.serviceid
where (serv.servicename = 'service 1')
and s.SiteType = 1
and s.CustomerNumber = c.CustomerNumber
For XML PATH ('')
), 2, 1000) [Sites]
From dbo.customer c
INNER JOIN site as s
on c.customernumber = s.customernumber
INNER JOIN dbo.service as serv
on s.serviceid = serv.serviceid
INNER JOIN dbo.TechContact as tc
on tc.CustomerNumber = c.CustomerNumber
where (serv.servicename = 'service 1')
and s.SiteType = 1
這給了我這樣的結果:這是完美的:
Customer Sites Email
1 Site A [email protected]
2 Site B [email protected];[email protected];[email protected]
3 Site C, Site D [email protected];[email protected]
但是如果我想通過輸入獲得來自多個服務的結果或在兩者WHERE語句線,像這樣
where (serv.servicename = 'service 1' or serv.servicename = 'service 2')
那麼結果就不會串連那裏有來自不同的服務同一個客戶兩個站點:
Customer Site Email
1 Site A [email protected]
1 Site E [email protected]
2 Site B [email protected];[email protected];[email protected]
3 Site C, Site D [email protected];[email protected]
我不太確定它爲什麼不連接?我想我可能會在哪裏做錯了?
謝謝,我不認爲「爲Table」是正確的語法? – Jimms 2014-10-08 01:54:43
是的,我認爲是這樣的:/你可以複製整個第一個查詢,而不是使用table1,看看它是否工作。否則,我會看看那個明天,因爲我在牀上atm:頁 – Alex 2014-10-08 02:00:50