我在使用 XML 的 SQLCommand 时遇到问题。其实我是新手
我有 2 张 table :
表
Customer_BSM
:id_Cust | Name | Omset | Colour | seq --------------------------------------------- 1001 | Ali | 10 | Black | 1 2001 | Bhudi | 5 | Red | 2 3001 | charlie | 20 | yellow | 3
table
address_BSM
:id_cust | Address | no1 | no2 ----------------------------------- 1001 | Jln 1 | 1A | 2A 1001 | Jln 2 | 1B | 2B 2001 | street 1 | S1 | S2 3001 | gang A | AG | BG
Now, I want to select and create xml with sql command, especially with "for xml". How to write the sql command?
the result, maybe like this
<customer_BSM id="1">
<id_cust>1001<id_cust>
<Name>Ali</Name>
<omset>10</omset>
<colour>black</colour>
<addres_bsm>
<address>jln 1</address>
<no1>1A</no1
<no2>2A</no2>
</addres_bsm>
<addres_bsm>
<address>jln 2</address>
<no1>1B</no1
<no2>2B</no2>
</addres_bsm>
</customer_BSM>
<customer_BSM id="2">
<id_cust>2001<id_cust>
<Name>Bhudi</Name>
<omset>5</omset>
<colour>red</colour>
<addres_bsm>
<address>street 1 </address>
<no1>S1</no1
<no2>S2</no2>
</addres_bsm>
</customer_BSM>
<customer_BSM id="3">
<id_cust>3001<id_cust>
<Name>Charlie</Name>
<omset>20</omset>
<colour>Yellow</colour>
<addres_bsm>
<address>gang A</address>
<no1>AG</no1
<no2>BG</no2>
</addres_bsm>
</customer_BSM>
我使用了这个 SQL 命令,但不起作用
SELECT *, (
SELECT *
FROM [address_bsm] A INNER JOIN [customer_bsm] B
ON a.id_cust = b.id_cust
FOR XML PATH ('Address_bsm'))
FROM [customer_bsm]
FOR XML PATH
其实结果和我的目的不太一样。
最佳答案
我只能使用 MSSQL 进行测试,但 SQL 应该非常相似。
SELECT c.[id_Cust]
,[Name]
,[Omset]
,[Colour]
,[seq]
,(SELECT a.[Address],a.[no1],a.[no2] FROM address_BSM a
WHERE a.id_cust = c.id_cust
FOR XML PATH ('address_bsm'), TYPE)
FROM [dbo].[Customer_BSM] c
FOR XML PATH ('customer_bsm')
关于mysql - 使用 SQL 命令编写 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32325830/