select column_name from information_schema.columns where table_schema='cmbscan' and table_name='tmp_20160512084827';
+-------------+
| column_name |
+-------------+
| ip          |
| port        |
| state       |
| name        |
| strtime     |
| elapsed     |
+-------------+这个是获取到的表的列名,但是竖向的。现在想要将其横向,能够使用union select将数据拼接在一起。
各位大神,请帮忙看看该如何实现呢

解决方案 »

  1.   


    如果你要横行,就是行专列,类似这样:select max(column_name='ip' then column_name end) ip,
           max(column_name='port' then column_name end) ip,
           ...
           
    from information_schema.columns 
    where table_schema='cmbscan' and table_name='tmp_20160512084827'
    group by table_name;
      

  2.   

    mysql 的吗? mysql 
    select group_concat(column_name) from information_schema.columns where table_schema='cmbscan' and table_name='tmp_20160512084827';
      

  3.   


    测试有效,但是用union 合并的时候还是报了错
    select group_concat(column_name) from information_schema.columns where table_schema='cmbscan' and table_name='tmp_20160512084827' union select * from tmp_20160512084827;
    ERROR 1222 (21000): The used SELECT statements have a different number of columns最后,我用最low的办法,select ‘columns1','columns2'  from table_name  来把列名加上了
      

  4.   

    如果这样你尝试用union 做成派生表嘛,就可以避开这个问题啦
      

  5.   

    而且这个提示是说union的列数不一致,并不是说函数本身有问题,估计你要看下 select * from tmp_20160512084827 到底有多少列
      

  6.   


    最初的计划就是想用union 来派生出带列名的一个csv文件。但是找出的列名是竖向排列的,所以就来问问大神这个怎么能横向。这个列数应该是不会有问题的,我的表里面就那么几列。真的很感谢您能帮我解答这个问题