简体   繁体   中英

MySql #1054 error while executing it through phpMyAdmin

I am trying to execute this code in phpMyAdmin. But, I'm getting following error

#1054 - Unknown column 'year' in 'field list'

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;

Check out fiddle for implementation.

How to fix this? And any ideas how do I script in PHP?

Year is a known object in MYSQL, if you have a column named 'year', try it with a back-tick '`'::

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM