2017-05-19 28 views
1

我有一個3行的表,1被用作行標識符,第2個被用來做與其他表的關係,第3個擁有內容。我正在做一個查詢,其中給定的結果是我需要的,但有一點點的錯誤。無法管理從數據庫導出多行到XML

表:print

下面是我使用的查詢:

SELECT Plate, tbl_veiculos.ID, Section, Category, Brand, Model, Version, Fuel, Price, B2BPrice, Year, Month, TollClass, Origin, Color, SeatColour, Seats, Kms, Doors, HP, Owners, CC, Obs, TaxDeductible, WarrantyMonth, 
(SELECT Name, Street, Locality, Email from tbl_AdStand where tbl_AdStand.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 FOR XML PATH('AdStand'), type), 
(SELECT DISTINCT Site from tbl_ExportSites where tbl_ExportSites.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 FOR XML PATH('ExportSites'), type) 
FROM tbl_veiculos, tbl_veiculo_spec, tbl_AdStand 
WHERE tbl_veiculos.ID = tbl_veiculo_spec.ID AND tbl_AdStand.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 
FOR XML PATH ('Vehicle'), TYPE, ROOT('VehicleList') 

下面是我得到的輸出:

<VehicleList> 
    <Vehicle> 
    <Plate>34-23-nd</Plate> 
    <ID>1</ID> 
    <Section>carros</Section> 
    <Category>cabrio</Category> 
    <Brand>Mercedes-Benz</Brand> 
    <Model>A140</Model> 
    <Version>1.4 twin-turbo</Version> 
    <Fuel>Gasolina</Fuel> 
    <Price>18.000</Price> 
    <B2BPrice>0</B2BPrice> 
    <Year>2015</Year> 
    <Month>03</Month> 
    <TollClass>2</TollClass> 
    <Origin>Alemanha</Origin> 
    <Color>Vermelho</Color> 
    <SeatColour>Cinza</SeatColour> 
    <Seats>5</Seats> 
    <Kms>13000</Kms> 
    <Doors>5</Doors> 
    <HP>310</HP> 
    <Owners>0</Owners> 
    <CC>1.389</CC> 
    <Obs>Tinta raspada no para-choques dianteiro</Obs> 
    <WarrantyMonth>0</WarrantyMonth> 
    <AdStand> 
     <Name>Stand Veloso</Name> 
     <Street>Rua dos Biscainhos</Street> 
     <Locality>2450-341</Locality> 
     <Email>[email protected]</Email> 
    </AdStand> 
    <ExportSites> 
     <Site>Coisas</Site> 
    </ExportSites> 
    <ExportSites> 
     <Site>Custojusto</Site> 
    </ExportSites> 
    <ExportSites> 
     <Site>StandVirtual</Site> 
    </ExportSites> 
    </Vehicle> 
</VehicleList> 

的 「錯誤」 是在上線

 <ExportSites> 
       <Site>Coisas</Site> 
      </ExportSites> 
      <ExportSites> 
       <Site>Custojusto</Site> 
      </ExportSites> 
      <ExportSites> 
       <Site>StandVirtual</Site> 
      </ExportSites> 

輸出apears「分離」,而我需要absolutelly相反,我需要它看起來像這樣[硬編碼]:

  <ExportSites> 
       <Site>Coisas</Site> 
       <Site>Custojusto</Site> 
       <Site>StandVirtual</Site> 
      </ExportSites> 

什麼我需要在我的查詢來改變,讓我得到這個。另外,我可以稍後使用PHP將此查詢導出爲XML文件嗎?

謝謝你的時間。

+0

下一次,當你把一個問題,請把它降低到最低限度所需。所有不需要指出問題的列都應該刪除。只是讓其中一個或兩個到位以顯示結構... – Shnugo

回答

2

試試這個:

DECLARE @mockup TABLE([Site] VARCHAR(100)) 
INSERT INTO @mockup VALUES ('Site 1'),('Site 2'); 

SELECT [Site] FROM @mockup FOR XML PATH('ExportSites'),TYPE; 
SELECT [Site] FROM @mockup FOR XML PATH(''),ROOT('ExportSites'),TYPE; 

結果

<ExportSites> 
    <Site>Site 1</Site> 
</ExportSites> 
<ExportSites> 
    <Site>Site 2</Site> 
</ExportSites> 

<ExportSites> 
    <Site>Site 1</Site> 
    <Site>Site 2</Site> 
</ExportSites> 

你看到區別?

PATH()將指定該行的名稱,而ROOT將定義一個額外的封閉元素。因爲你行的名稱爲空,則列名是單獨 ...

只是出於好奇:你可以嘗試一個更列添加到樣機上述 -table和比較差...

因此改變你的子查詢:

(SELECT DISTINCT Site 
from tbl_ExportSites 
where tbl_ExportSites.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 
FOR XML PATH(''),ROOT('ExportSites'), type) 
+0

非常感謝,這對我有很大的幫助。 –

0

我認爲你需要提供如下根:

SELECT Plate, tbl_veiculos.ID, Section, Category, Brand, Model, Version, Fuel, Price, B2BPrice, Year, Month, TollClass, Origin, Color, SeatColour, Seats, Kms, Doors, HP, Owners, CC, Obs, TaxDeductible, WarrantyMonth, 
(SELECT Name, Street, Locality, Email from tbl_AdStand where tbl_AdStand.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 FOR XML PATH('AdStand'), type), 
(SELECT DISTINCT Site from tbl_ExportSites where tbl_ExportSites.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 FOR XML PATH('Site'), type, root('ExportSites')) 
FROM tbl_veiculos, tbl_veiculo_spec, tbl_AdStand 
WHERE tbl_veiculos.ID = tbl_veiculo_spec.ID AND tbl_AdStand.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 
FOR XML PATH ('Vehicle'), TYPE, ROOT('VehicleList') 
+0

不建議使用笛卡爾積而不使用顯式連接。使用明確連接 –

+0

我試過,但都沒有成功,它的輸出這個 ' Coisas Custojusto StandVirtual ' –

+0

你在正確的軌道上建議添加一個「ROOT」元素,但是你仍然有一個在PATH()中定義的行名... – Shnugo