我在将数据透视表查询导出到“ .csv”时遇到问题。该查询工作正常,但是当我尝试导出它时出现错误。
这是我的PHP导出代码:
<?php
if(isset($_POST["export"]))
{
$connect = mysqli_connect("localhost", "root", "", "borok");
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=borok.csv');
$output = fopen("php://output", "w");
fputcsv($output, array('user_id', 'wine_id', 'pontszam'));
$query = "SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'ifnull(SUM(case when wine_id = ''',
wine_id,
''' then pontszam end),0) AS `',
wine_id, '`'
)
) INTO @sql
FROM
ertekel;
SET @sql = CONCAT('SELECT user_id, ', @sql, '
FROM ertekel
GROUP BY user_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($output, $row);
}
fclose($output);
}
我想要这样的导出表:
| | wine_id1 | wine_id2 | wine_id3 |
-----------------------------------------------------------------------------
| user_id1 | 78 | 0 | 77 |
| user_id2 | 0 | 11 | 81 |
| user_id3 | 44 | 66 | 66 |
-----------------------------------------------------------------------------
当我尝试导出表时,出现此错误:
您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以获取在'SET @sql = CONCAT('SELECT user_id'@sql'FROM ertekel
我该怎么办?