[英]mysql create temp table column using select command
i need to create a temporary table which column name will generate dynamically using a select command (mean: the value of another table.).我需要创建一个临时表,其列名将使用选择命令动态生成(意思是:另一个表的值。)。 But when i run the command then the temp table created with the value not the column name.但是当我运行该命令时,创建的临时表的值不是列名。 i have tried as below:我试过如下:
SELECT ColVal FROM tableA SELECT ColVal FROM tableA
TableA表A
------------------------
ColName | ColVal
------------------------
id | 1
----------------------
Name | Test
----------------------
Age |25
---------------------
Now , i need to create Temp table B which column name will be as below现在,我需要创建临时表 B,列名如下
TableB表B
--------------
id| Name| Age|
--------------
I have tried by the query:我已经通过查询尝试过:
CREATE TEMPORARY TABLE TableB as (select ColName from TableA)
it's not give me the actual output.它没有给我实际的输出。 it create the table like:它创建表,如:
TableB表B
-----------------
ColName
-----------------
id
--------
Name
--------
Age
--------
I'm not sure that it is the best way to do it, but you can run dynamically constructed sql.我不确定这是最好的方法,但是您可以运行动态构造的 sql。 You can do something like that:你可以这样做:
SELECT CONCAT("CREATE TEMPORARY TABLE TableB (",
group_concat( CONCAT(ColName, " VARCHAR(50)")), ");")
INTO @Expression
FROM TableA;
PREPARE myquery FROM @Expression;
EXECUTE myquery;
@Expression will be equal to: CREATE TEMPORARY TABLE TableB (id VARCHAR(50),Name VARCHAR(50),Age VARCHAR(50));
@Expression 将等于: CREATE TEMPORARY TABLE TableB (id VARCHAR(50),Name VARCHAR(50),Age VARCHAR(50));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.