我正在尝试在 phpMyAdmin 中执行此代码。但是,我收到以下错误
#1243 - 给 EXECUTE 的未知准备好的语句处理程序 (stmt)
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(distinct CASE WHEN topic = ''',
topic,
''' THEN t2.id END) AS `',
topic, '`'
)
) INTO @sql
FROM
(
select count(distinct id) total, topic
from table2
group by topic
order by total desc
limit 2
) d;
SET @sql
= CONCAT('SELECT t1.year, ', @sql, '
from table1 t1
left join table2 t2
on t1.id = t2.id
group by t1.year');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
查看fiddle的实现。
如何解决这个问题?此外,正如评论员所提到的,这将在通过脚本发送时起作用。任何想法如何在 PHP 中编写脚本?