-1
別名已給出區分表的兩個副本。 如何將以下三個查詢合併爲一個?我需要使用這些數據創建一個菜單。像Country - > State - > Cites一樣的分層數據結構。使用SELF JOIN寫入查詢
以下是列的int表:
[ID]
,[Type]
,[ParentID]
,[Title]
,[Name]
,[ZoneName]
,[TemplateKey]
,[TranslationKey]
,[Created]
,[Updated]
,[Published]
,[Expires]
,[SortOrder]
,[Visible]
,[VersionOfID]
,[SavedBy]
,[AncestralTrail]
,[VersionIndex]
,[State]
,[ChildState]
,[AlteredPermissions]
SELECT a.[ID],a.[Type],a.[ParentID],a.[Title],b.[ID],b. [Type],b.[ParentID],b.[Title], a.[AncestralTrail]
FROM [n2Item] a, [n2Item] b
WHERE a.[ID] = b.[ParentID] AND a.Title = 'Holidays' AND b.[Type] = 'StorefrontPage'
ORDER BY b.[Title] ,A.[ParentID],a.[SortOrder];
結果: 810,主頁,1,假期,5531,StorefrontPage,810,加拿大,/ 1/
SELECT a.[ID],a.[Type],a.[ParentID],a.[Title],b.[ID],b. [Type],b.[ParentID],b.[Title], a.[AncestralTrail]
FROM [n2Item] a, [n2Item] b
WHERE a.[id] = b.[ParentID] AND a.Title = 'Canada'
ORDER BY b.[Title] ,A.[ParentID],a.[SortOrder];
結果: 5531,StorefrontPage,810,加拿大,6132,ImportedPage,5531 2014特別優惠,/ 1/810/
SELECT a.[ID],a.[Type],a.[ParentID],a.[Title],b.[ID],b. [Type],b.[ParentID],b.[Title], a.[AncestralTrail]
FROM [n2Item] a, [n2Item] b
WHERE a.[id] = b.[ParentID] AND a.Title = 'Vancouver'
ORDER BY b.[Title] ,A.[ParentID],a.[SortOrder];
結果: 5542,StorefrontPage,5531,溫哥華,6365,ImportedPage,5542,阿拉斯加克魯斯和溫哥華Stay,/ 1/810/5531/ 5542,StorefrontPage,5531,Vancouver,6368,ImportedPage,5542,Best Western Plus Sands Hotel,/ 1/810/5531/
您可以'聯合所有'來合併它們 - 作爲一個條件,您需要使所有這3個查詢返回相同數量的列。 – Milen