开发者

How to generate xml from php with mysql group column?

开发者 https://www.devze.com 2023-02-08 12:35 出处:网络
I have table (electronics): ca开发者_如何学编程tegory|product --------------------------- Audiomp3player

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

精彩评论

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