0

我正在尝试在 phpMyAdmin 中执行此代码。但是,我收到以下错误

#1054 - “字段列表”中的未知列“年份”

SELECT
  CONCAT('SELECT year2,',
  GROUP_CONCAT(sums),
  ' FROM yourtable GROUP BY year2')
FROM (
SELECT CONCAT('SUM(name=\'', name, '\') AS `', name, '`') sums
FROM yourtable
GROUP BY name
ORDER BY COUNT(*) DESC
) s
INTO @sql;

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

查看fiddle的实现。

如何解决这个问题?以及如何在 PHP 中编写脚本的任何想法?

4

1 回答 1

1

Year是 MYSQL 中的一个已知对象,如果你有一个名为 'year' 的列,请尝试使用反引号 '`'::

SELECT
  CONCAT('SELECT `year`,',
  GROUP_CONCAT(sums),
  ' FROM yourtable GROUP BY `year`')
FROM (
SELECT CONCAT('SUM(name=\'', name, '\') AS `', name, '`') sums
FROM yourtable
GROUP BY name
ORDER BY COUNT(*) DESC
) s
INTO @sql;

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
于 2013-07-02T07:11:39.463 回答