Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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查询,根据id将一列拆分为多个单独的列_Mysql - Fatal编程技术网

mysql查询,根据id将一列拆分为多个单独的列

mysql查询,根据id将一列拆分为多个单独的列,mysql,Mysql,我有一张表,如下所示: +-----------+-----------+--------+ | ID | Name | Format | +-----------+-----------+--------+ | 1 | Rama | Text | +-----------+-----------+--------+ | 2 | Sita | Text | +-----------+-----------+---

我有一张表,如下所示:

+-----------+-----------+--------+
|  ID       | Name      | Format |
+-----------+-----------+--------+
| 1         | Rama      | Text   |
+-----------+-----------+--------+
| 2         | Sita      | Text   |
+-----------+-----------+--------+
| 1         | Raja      | Text   |
+-----------+-----------+--------+
| 2         | Bheem      | Text   |
+-----------+-----------+--------+
我想要上表的以下输出:

+-----------+-----------+
|  ID_1     | ID_2      |
+-----------+-----------+
| Rama      | Sita      |
+-----------+-----------+
| Raja      | Bheem     |
+-----------+-----------+
有人能给我mysql查询来做这个吗


提前谢谢。

您可以尝试此查询

SELECT  ID,
        MAX(IF(`ID` = 1, Name, NULL)) ID_1,
        MAX(IF(`ID` = 2, Name, NULL)) ID_2
FROM    Table
GROUP   BY ID
遵循与您的要求类似的FIDLE代码。

您如何知道哪个id 1与哪个id 2记录配对?到目前为止,您尝试了什么?我已经编写了两个单独的查询来提取数据。如从表1中选择名称作为ID_1,其中ID=1;是否可以为此获取单个查询,或者我需要加入两个子查询。您尚未回答我的问题。无需配对。。我只需要两列,一列用于Id 1,另一列用于Id 2。你为什么使用max()?@Shadow:不用
max
就可以尝试一下,然后你就可以理解他为什么使用
max()