Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.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 透视SQL数据-将行分组为列_Mysql_Wordpress_Pivot - Fatal编程技术网

Mysql 透视SQL数据-将行分组为列

Mysql 透视SQL数据-将行分组为列,mysql,wordpress,pivot,Mysql,Wordpress,Pivot,我将数据以以下格式存储在mySQL数据库中: +------------+------------+-----------+ | id | field | value | +============+============+===========+ | 1 | first | Bob | +------------+------------+-----------+ | 1 | last |

我将数据以以下格式存储在mySQL数据库中:

+------------+------------+-----------+
|    id      |   field    |   value   |
+============+============+===========+
|     1      |   first    |    Bob    |
+------------+------------+-----------+
|     1      |   last     |   Smith   |
+------------+------------+-----------+
|     2      |   first    |    Jim    |
+------------+------------+-----------+
|     2      |   last     |   Jones   |
+------------+------------+-----------+
我希望它按如下方式返回:

+------------+------------+-----------+
|    id      |   first    |   last    |
+============+============+===========+
|     1      |    Bob     |   Smith   |
+------------+------------+-----------+
|     2      |    Jim     |   Jones   |
+------------+------------+-----------+
我知道这似乎是一种愚蠢的数据存储方式,但这只是我真正拥有的一个简单例子。这个表格是用WordPress插件的这种方式格式化的,我想让它在不重写插件的情况下工作

据我所知,我无法将PIVOT与mySql结合使用。有没有类似于PIVOT的东西可以用来实现我的目标?

试试这个PIVOT查询:

SELECT id,
    MAX(CASE WHEN field = 'first' THEN value ELSE NULL END) AS first,
    MAX(CASE WHEN field = 'last'  THEN value ELSE NULL END) AS last
FROM yourTable
GROUP BY id
按照下面的链接进行运行演示:

尝试此数据透视查询:

SELECT id,
    MAX(CASE WHEN field = 'first' THEN value ELSE NULL END) AS first,
    MAX(CASE WHEN field = 'last'  THEN value ELSE NULL END) AS last
FROM yourTable
GROUP BY id
按照下面的链接进行运行演示:

试试这个;)

试试这个;)


可能重复的工作很好!非常感谢,非常好!非常感谢。