dpleylxzx47207117 2011-05-16 12:34
浏览 58
已采纳

如何在行和列反转时显示数据库中的数据


I have to deal with a table like this one with approximately a couple of hundreds of #id (and counting).

id     field_name     field_value  
1      name           pasta  
1      lat            41.27651  
1      long           11.45234  
2      name           pizza  
2      lat            43.53455 
2      long           12.62346
..     ....           ........
..     ....           ........
..     ....           ........

I need to build an XML file that allows me to add markers on Google Map with a structure like this:

<makers>
    <marker name="$name" lat="$lat" long="$long" />
    <marker name="$name" lat="$lat" long="$long" />
    (...)
</markers>

I am trying to do a query like this and then put all the info in arrays and play with some foreach but I am failing so hard that I can't go on.

$query = "  SELECT field_val
        FROM table
        WHERE field_name='Name'";

The main problem is that i am not able to work with a structure like this, given the fact that a probably better structure would have been like this

id    name    lat       long
1     pasta   41.27651  11.45234
2     pizza   43.53455  12.62346
..    ...     ...       ...

Please note that i can't change the way the entries go in the DB and that the structure of the table is more complicated than the one showed here (with approx. 15 field_name per id, not all of them necessary).

Sorry for the poor english and thanks for your time.

  • 写回答

3条回答 默认 最新

  • doujia6433 2011-05-16 12:58
    关注
    SELECT 
      id,
      GROUP_CONCAT(if(fieldname='name', field_value, NULL)) AS 'name', 
      GROUP_CONCAT(if(fieldname='lat', field_value, NULL)) AS 'lat', 
      GROUP_CONCAT(if(fieldname='long', field_value, NULL)) AS 'long' 
    FROM table 
    GROUP BY id; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真