问题
i have a pivot table code that worked in the sqlfiddle, I can't seem to get the query working when I bring it back onto my database in phpmyadmin.
Here is my sqlfiddle.
when I run it in my localHost phpmyadmin, the query becomes something like below:
SET @@group_concat_max_len = 5000;# MySQL returned an empty result set (i.e. zero rows).
SET @sql = NULL;# MySQL returned an empty result set (i.e. zero rows).
SELECT GROUP_CONCAT(DISTINCT
CONCAT('MAX(IF(hw_id = ', hw_id, ', score, -1)) AS ',
CONCAT('`hw', hw_id, '`'))) INTO @sql
FROM mk;# 1 row affected.
SET @sql = CONCAT('SELECT st.id, ', @sql,
' FROM st LEFT JOIN mk ON st.id = mk.st_id GROUP BY st.id');
# MySQL returned an empty result set (i.e. zero rows).
PREPARE stmt FROM @sql;# MySQL returned an empty result set (i.e. zero rows).
EXECUTE stmt;# Rows: 3
DEALLOCATE PREPARE stmt;# MySQL returned an empty result set (i.e. zero rows).
as you can see, I change the group_concat_max_len too... but nothing changed...
回答1:
Build it into a stored procedure, then execute that in phpmyadmin.
If you want to pursue the failing phpadmin, add SELECT @sql; and see if you get the desired query.
回答2:
While analyzing the phpMyAdmin bug reported for this question, I've realized that this query works on some servers and fails on others, so it's rather MySQL server incompatibility than an issue in phpMyAdmin. Different MySQL server versions behave differently in this case (which strictly speaking doesn't follow grammar defined by MySQL documentation).
I've tested:
- in MySQL 5.5.46 the query fails in both phpMyAdmin and commandline
- in MariaDB 10.0.24 the query works fine in both phpMyAdmin and commandline
来源:https://stackoverflow.com/questions/35667444/mysql-pivot-table-working-in-sqlfiddle-but-not-working-in-phpmyadmin