如何从准备好的语句(Select 查询)创建视图或 Table

How to Create View or Table from Prepared Statement (Select Query)

我正在尝试从以下准备好的语句创建视图:

CREATE VIEW myview AS -- this line is not working
SELECT CONCAT(GROUP_CONCAT('SELECT ''', COLUMN_NAME,''' MyColumns, SUM(`', COLUMN_NAME,'`) Total FROM mydb.source_table' SEPARATOR '\n UNION ALL \n'),'\nORDER BY Total ASC')
INTO @sql
FROM  INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'mydb' 
    AND TABLE_NAME   = 'source_table'
    AND COLUMN_NAME NOT IN ('ID', 'Name');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt;

我仍然对处理 group_concat 语法感到困惑。

我只需要有关如何使用上述准备好的语句创建视图的帮助。

更新 1: 存储过程

按照@nbk 的建议,我必须在单列中创建 5 个存储过程才能使用数据并将其用于 CREATE TABLE 查询。这是上述代码的存储过程版本。

CREATE DEFINER=`admin`@`%` PROCEDURE `sp_result`()
BEGIN
    SELECT CONCAT(GROUP_CONCAT('SELECT ''', COLUMN_NAME,''' MyColumns FROM mydb.source_table' SEPARATOR '\n UNION ALL \n'), '\n LIMIT 0, 1 \n')
    INTO @sql
    FROM  INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'mydb' 
        AND TABLE_NAME   = 'source_table'
        AND COLUMN_NAME NOT IN ('ID', 'Name');

    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DROP PREPARE stmt;
END

这是此存储过程的结果:

+-----------+
| MyColumns |
+-----------+
| Abc       |
+-----------+

为了让这种方法对我有用,我希望上面的结果成为我的 CREATE TABLE 查询的列之一:

这是我的尝试,但没有成功:

第一次尝试:分离查询

CREATE TABLE my_table AS SELECT id, name, mydb.sp_result() FROM source_table; -- Error Code: 1305. FUNCTION project_x.best_vendor1_name does not exist

第二次尝试:插入存储过程的最后一部分

CREATE TABLE my_table AS SELECT @sql FROM source_table; -- no effect

这里,真不知道CREATETABLE是如何使用存储过程返回数据的。

更新 2: 说明封装

CREATE DEFINER=`root`@`%` PROCEDURE `proc_column_sum`()
BEGIN
DROP TABLE IF EXISTS table2;
SELECT CONCAT('
    CREATE TABLE table2 AS (',GROUP_CONCAT('
        SELECT ''', COLUMN_NAME,''' MyColumns, SUM(`', COLUMN_NAME,'`) Total 
        FROM testdb.products ' 
        SEPARATOR '\n UNION ALL \n'), -- runtime syntax error somewhere here
    '\n ORDER BY Total ASC)') -- missing closing single quote right after ASC
INTO @sql
FROM  INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'testdb' 
    AND TABLE_NAME   = 'products'
    AND COLUMN_NAME NOT IN ('ID', 'Name');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt;
END

您必须将 CREATE TABLE 放入准备好的语句中。

此示例使用临时 table 仅用于演示目的

CREATE DEFINER=`root`@`%` PROCEDURE `proc_column_sum`()
BEGIN
SELECT 
CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (',GROUP_CONCAT('SELECT ''', COLUMN_NAME,''' MyColumns, SUM(`', COLUMN_NAME,'`) Total FROM testdb.products ' SEPARATOR '\n UNION ALL \n'),'\nORDER BY Total ASC)')
INTO @sql
FROM  INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'testdb' 
    AND TABLE_NAME   = 'products'
    AND COLUMN_NAME NOT IN ('ID', 'Name');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt;
END

那你就叫它

call proc_column_sum();
SELECT * FROM table2

但是使用这种方法,您每次都调用程序来获取最新数据。

我不知道,你的查询出了什么问题

CREATE DEFINER=`root`@`%` PROCEDURE `proc_column_sum`()
BEGIN
DROP TABLE IF EXISTS table2;
SELECT CONCAT('
    CREATE TABLE IF NOT EXISTS table2 AS (',GROUP_CONCAT('
    SELECT ''', COLUMN_NAME,''' MyColumns, SUM(`', COLUMN_NAME,'`) Total 
    FROM testdb.products ' 
    SEPARATOR '\n UNION ALL \n')
    ,'\nORDER BY Total ASC)')
INTO @sql
FROM  INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'testdb' 
    AND TABLE_NAME   = 'products'
    AND COLUMN_NAME NOT IN ('ID', 'Name');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt;
END