嗨我有这样的东西作为更复杂查询的一部分:
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
我不想执行stmt本身,而是想从执行结果中创建一个表。
Create table A as select * from (Execute stmt)
给我错误
编辑: 这是整个过程:
Use catdatabase;
SET SESSION group_concat_max_len = 1000000;
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(CASE WHEN columnA = "' ,columnA, '"THEN 1 ELSE 0 end) AS "' ,columnA, '"'))
INTO @sql
FROM
tableB;
SET @sql = CONCAT('SELECT columnB, Count(*) total, ', @sql, '
FROM tableA inner join tableB on tableA.columnC = tableB.columnE where tableA.columnD <> "catpoop"
GROUP BY columnB');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
答案 0 :(得分:0)
set @table='t2';
SET @table1 = 'test';
set @a = concat('create table ',@table,' select * from ',@table1);
prepare stmt1 from @a;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
https://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
更新
SET SESSION group_concat_max_len = 1000000;
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(CASE WHEN columnA = "' ,columnA, '"THEN 1 ELSE 0 end) AS "' ,columnA, '"'))
INTO @sql
FROM
tableB;
SET @sql = CONCAT('SELECT columnB, Count(*) total, ', @sql, '
FROM tableA inner join tableB on tableA.columnC = tableB.columnE where tableA.columnD <> "catpoop"
GROUP BY columnB');
set @new_sql = concat ('create table A',@sql);
PREPARE stmt FROM @new_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
答案 1 :(得分:0)
正如我所见,您想要转移数据并希望保存到临时表中。 我已更新您的查询,请查看此内容:
Use catdatabase;
SET SESSION group_concat_max_len = 1000000;
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(CASE WHEN columnA = "' ,columnA, '"THEN 1 ELSE 0 end) AS "' ,columnA, '"'))
INTO @sql
FROM
tableB;
SET @sql = CONCAT('Create temporary table temp SELECT columnB, Count(*) total, ', @sql, ' FROM tableA inner join tableB on tableA.columnC = tableB.columnE where tableA.columnD <> "catpoop"
GROUP BY columnB');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
如果您收到任何错误,请告诉我。