我有这个问题:
select f.n, t.l, grp.n, cat.n, col.n, col_value
from fc
inner join f on f.idfc = fc.idfc
inner join t on t.idf = f.idf
inner join grp on grp.idt = t.idt
inner join cat on cat.idgrp = grp.idgrp
inner join col on col.idcat = cat.idcat
inner join col_value on col_value.idcol = col.idcol
for xml auto, root('fc')
;
返回此xml:
<fc>
<f n="341.11.7">
<t l="A">
<grp n="ID">
<cat n="10">
<col n="R">
<col_value col_value="341" />
</col>
<col n="S">
<col_value col_value="11" />
</col>
<col n="H">
<col_value col_value="7" />
</col>
</cat>
</grp>
</t>
</f>
</fc>
但我需要col_value元素必须如下:
<col_value>341</col_value>
那么,如何修改查询以达到以下结果?
<fc>
<f n="341.11.7">
<t l="A">
<grp n="ID">
<cat n="10">
<col n="R">
<col_value>341</col_value>
</col>
<col n="S">
<col_value>11</col_value>
</col>
<col n="H">
<col_value>7</col_value>
</col>
</cat>
</grp>
</t>
</f>
</fc>
谢谢大家。 查尔斯。
答案 0 :(得分:0)
尝试更改您的查询:
select f.n, t.l, grp.n, cat.n, col.n, (select col_value for xml path,type).query('/row/col_value') )
from fc
inner join f on f.idfc = fc.idfc
inner join t on t.idf = f.idf
inner join grp on grp.idt = t.idt
inner join cat on cat.idgrp = grp.idgrp
inner join col on col.idcat = cat.idcat
inner join col_value on col_value.idcol = col.idcol
for xml auto, root('fc')
;
我希望这会有所帮助。
答案 1 :(得分:0)
select
f.n, t.l, grp.n, cat.n, col.n,
(select col_value for xml path(''), type)
from fc
inner join f on f.idfc = fc.idfc
inner join t on t.idf = f.idf
inner join grp on grp.idt = t.idt
inner join cat on cat.idgrp = grp.idgrp
inner join col on col.idcat = cat.idcat
inner join col_value on col_value.idcol = col.idcol
for xml auto, root('fc')
<强> sql fiddle demo 强>