Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用列中的值作为新表的字段名_Mysql - Fatal编程技术网

Mysql 使用列中的值作为新表的字段名

Mysql 使用列中的值作为新表的字段名,mysql,Mysql,我需要构建一个临时表,其中字段名是另一个表的列中的值。在网站开发过程中,第二个表可以(也将)更改,所以我不能将字段名硬编码到我的例程中 作为我的意思的一个例子,将其视为源表,恰当地命名为“SuxEdabl”: 然后我需要制作一张如下所示的桌子: profile_name profile_surname profile_address_street John Doe Baker Street 我试过这个: create temporary ta

我需要构建一个临时表,其中字段名是另一个表的列中的值。在网站开发过程中,第二个表可以(也将)更改,所以我不能将字段名硬编码到我的例程中

作为我的意思的一个例子,将其视为源表,恰当地命名为“SuxEdabl”:

然后我需要制作一张如下所示的桌子:

profile_name    profile_surname profile_address_street
John            Doe             Baker Street
我试过这个:

create temporary table if not exists newtable(
  select fieldname from sourcetable
);

但它返回的表只包含“sourcetable”中的“fieldname”列。有人知道一种干净、快速的方法吗?

在我看来,最简单、最干净的方法是在应用程序代码中使用
sourcetable
中的数据构建
CREATE[TEMPORARY]TABLE
(或者甚至是一个普通的
SELECT
查询,具体取决于您如何使用这些东西)语句,然后运行它


您将无法在纯SQL中灵活地执行任何操作,也可能无法使用存储过程(但我不是100%确定)。

嗨,Romain,感谢您的快速反应。事实上,我已经想出了类似的方法,但是项目的(稍微混乱的)混合技术方法要求所有这些都在一个存储过程中完成,而不是在应用程序逻辑中完成。
create temporary table if not exists newtable(
  select fieldname from sourcetable
);