开发者

How to generate xml from php with mysql group column?

I have table (electronics):

ca开发者_如何学编程tegory     |   product
---------------------------
  Audio         mp3player
  Video           dvd
  Audio         cdplayer
  Video          bluray
  null            lcd

I want output:

<cat label='Audio'>
      <pro label='mp3player' />
      <pro label='cdplayer' />
</cat>
<cat label='Video'>
      <pro label='dvd' />
      <pro label='bluray' />
</cat>
<pro label='lcd'/>

is that posible?

Note: this is for flex 3 tree component data.


You can get what you want by doing the following. This assumes your table name is catprod and your columns are cat and prod:

select concat('<cat label=''',cat,'''>'

    ,GROUP_CONCAT(concat('<prod label=''',prod,''' />') SEPARATOR '')

    ,'</cat>')

from catprod 

where cat is not null

group by cat

UNION

SELECT concat('<pro label=''',prod,'''/>') from catprod where cat is null;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜