欢笑:无法执行数据库查询

时间:2015-08-11 15:42:34

标签: mysql mirth

我的问题是:如果我将下面的查询字符串更改为select * from table;,它可以正常工作。但如果我执行下面的查询字符串,则会抛出错误:

SQL error: Invalid column number. Cannot less than or equal zero.

这是代码:

columnHeader = " 'id', 'pat_name', 'pat_age' ";
var filename = $('serType') + $('serId') + ".csv"; 

    var reporting_exportQuery = "select " + columnHeader + 
                        " union all " + 
                        "select * " +
                        "from " + $('serType') + 
                        " into outfile 'C://mytest/reports/service reports/"+$('serType') + "/" + "" + filename + "' " +
                        "fields terminated by ',' " +
                        "lines terminated by '\\n';";           

    var test = "select * from Age;";
    reporting_DBConn = DatabaseConnectionFactory.createDatabaseConnection('com.mysql.jdbc.Driver',:D);  
    logger.info(reporting_exportQuery);
    var result = reporting_DBConn.executeCachedQuery(reporting_exportQuery);

2 个答案:

答案 0 :(得分:0)

我的猜测columnHeader的联合部分中的列数不同于select *

步骤调试。

  • 打印结果字符串reporting_exportQuery
  • 尝试在MySql上运行该字符串。

已添加

  • First Select没有FROM部分

 var reporting_exportQuery = 
             "select " + columnHeader + 
             "FROM SomeTable"    <--- need FROM
             " union all " + 
             "select " + columnHeader + <--- need same columns

答案 1 :(得分:0)

您正在为表中的所有列(*)添加(联合)一列columnHeader。你可以这样做

SELECT 'columnHeader',table_alias.* FROM TABLE table_alias...


var filename = $('serType') + $('serId') + ".csv"; 

var reporting_exportQuery = 
                    "select 'columnHeader',table_alias.* " +
                    "from " + $('serType') table_alias+ 
                    " into outfile 'C://mytest/reports/service reports/"+$('serType') + "/" + "" + filename + "' " +
                    "fields terminated by ',' " +
                    "lines terminated by '\\n';";           

var test = "select * from Age;";
reporting_DBConn = DatabaseConnectionFactory.createDatabaseConnection('com.mysql.jdbc.Driver','jdbc:mysql://localhost:3310/cnwlreports', :D);  
logger.info(reporting_exportQuery);
var result = reporting_DBConn.executeCachedQuery(reporting_exportQuery);