Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.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
Php 查询以获取结果中的列名和值?_Php_Mysql_Sql - Fatal编程技术网

Php 查询以获取结果中的列名和值?

Php 查询以获取结果中的列名和值?,php,mysql,sql,Php,Mysql,Sql,我有一个id为第一个字段的表,其他字段有描述字段。例如 id | color | length | year | land | name --------------------------------------------------- 3 | blue | long | 1988 | Netherlands | Jan 4 | yellow | short ..etc. 我想在每一行创建一个新表,其中包含以下字段:id、fieldname(来自我的第一个表)、该字段的值。所以像这样: 3

我有一个id为第一个字段的表,其他字段有描述字段。例如

id | color | length | year | land | name
---------------------------------------------------
3 | blue | long | 1988 | Netherlands | Jan
4 | yellow | short ..etc.
我想在每一行创建一个新表,其中包含以下字段:id、fieldname(来自我的第一个表)、该字段的值。所以像这样:

3 | color | blue
3 | length | long
3 | year | 1988
3 | land | Netherlands
3 | name | Jan
4 | color | yellow
4 | length | short
etc.
有人知道如何在查询中执行此操作吗?(还是在php脚本中?)


谢谢大家,阿里首先,您需要兼容的数据类型来完成您想要的任务。这通常是一个字符串

然后,您可以通过几种方式来实现这一点。MySQL中最简单的可能是
union all

select id, 'color' as which, color from t union all
select id, 'length' as which, cast(length as char) from t union all
. . .

列的数量是固定的,并且是预先知道的,对吗?