doujiu3095 2015-09-11 14:16
浏览 46
已采纳

使用php PDO执行mysql pivotable语句

I have Mysql prepare statement work in command line. how get I get it to work with PHP PDO prepare statement?

set @sql = null;
SET group_concat_max_len=15000;

select group_concat(distinct concat('sum(if(date=''',date_format(date,'%Y-%m-%d'),''',total,null)) as ', date_format(date,'dd%Y%m%d'))) into @sql from sales where date_format(date,'%m%y')='0615';

set @sql = concat('select ',@sql,'  from sales');

PREPARE stmt FROM @sql;
EXECUTE stmt;
  • 写回答

2条回答 默认 最新

  • doule0941 2015-09-11 14:30
    关注

    You can do the group concatenation in PHP instead of SQL.

    $pdo->query("SET group_concat_max_len = 15000");
    $stmt1 = $pdo->prepare("select distinct concat('sum(if(date=''',date_format(date,'%Y-%m-%d'),''',total,null)) as ', date_format(date,'dd%Y%m%d')) as col from sales where date_format(date,'%m%y')= :mmyy";
    $stmt1->execute(":mmyy" => '0615');
    $cols = $stmt1->fetchAll(PDO::FETCH_COLUMN, 0);
    $cols_string = implode(',' $cols);
    $sql = "SELECT $col_string FROM sales";
    $stmt2 = $pdo->prepare($sql);
    $stmt2->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?