使用Pivot在MySQL版本8.0.17中从不同表(union)进行行到列转换

使用Pivot在MySQL版本8.0.17中从不同表(union)进行行到列转换,mysql,sql,pivot,pivot-table,Mysql,Sql,Pivot,Pivot Table,我需要联合存储在MySql版本8.0.17上的两个不同的表,并在返回表上使用以下命令将行值设置为列名 第一个表格 +-----------------------+--------+-----+ | contents | sUnity | sID | +-----------------------+--------+-----+ | Set n.1 | Q400 | 83 | | - Par 1.1 | Q40

我需要联合存储在
MySql版本8.0.17
上的两个不同的表,并在返回表上使用以下命令将行值设置为列名

第一个表格

+-----------------------+--------+-----+
| contents              | sUnity | sID |
+-----------------------+--------+-----+
| Set n.1               | Q400   |  83 |
| - Par 1.1             | Q400   |  84 |
| <b>bold text</b>      | Q400   |  85 |
| - Par 1.2             | Q400   |  86 |
| normal text           | Q400   |  87 |
| Set n.2               | Q400   |  88 |
| - Par 2.1             | Q400   |  89 |
| <i>italic text</i>    | Q400   |  90 |
| - Par 2.2             | Q400   |  91 |
| <u>underline text</u> | Q400   |  92 |
| - Par 2.3             | Q400   |  93 |
+-----------------------+--------+-----+
11 rows in set (0.03 sec)
+-----------------------+--------+-----+
| contents              | sUnity | sID |
+-----------------------+--------+-----+
| Set n.1               | Q410   |  94 |
| - Par 1.1             | Q410   |  95 |
| <b>bold text</b>      | Q410   |  96 |
| - Par 1.2             | Q410   |  97 |
| normal text           | Q410   |  98 |
| Set n.2               | Q410   |  99 |
| - Par 2.1             | Q410   | 100 |
| <i>italic text</i>    | Q410   | 101 |
| - Par 2.2             | Q410   | 102 |
| <u>underline text</u> | Q410   | 103 |
| - Par 2.3             | Q410   | 104 |
+-----------------------+--------+-----+
11 rows in set (0.02 sec)
我的结构和数据表如下

-- ----------------------------
-- Table structure for t_contents_q400
-- ----------------------------
DROP TABLE IF EXISTS `t_contents_q400`;
CREATE TABLE `t_contents_q400`  (
  `contents` varchar(255) DEFAULT NULL,
  `sUnity` varchar(50) DEFAULT NULL,
  `sID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`sID`) USING BTREE,
  UNIQUE INDEX `contents`(`contents`, `sUnity`) USING BTREE
) ENGINE = InnoDB;

-- ----------------------------
-- Records of t_contents_q400
-- ----------------------------
INSERT INTO `t_contents_q400` VALUES ('- Par 1.1', 'Q400', 1);
INSERT INTO `t_contents_q400` VALUES ('- Par 1.2', 'Q400', 2);
INSERT INTO `t_contents_q400` VALUES ('- Par 2.1', 'Q400', 3);
INSERT INTO `t_contents_q400` VALUES ('- Par 2.2', 'Q400', 4);
INSERT INTO `t_contents_q400` VALUES ('- Par 2.3', 'Q400', 5);
INSERT INTO `t_contents_q400` VALUES ('<b>bold text</b>', 'Q400', 6);
INSERT INTO `t_contents_q400` VALUES ('<i>italic text</i>', 'Q400', 7);
INSERT INTO `t_contents_q400` VALUES ('<u>underline text</u>', 'Q400', 8);
INSERT INTO `t_contents_q400` VALUES ('normal text', 'Q400', 9);
INSERT INTO `t_contents_q400` VALUES ('Set n.1', 'Q400', 10);
INSERT INTO `t_contents_q400` VALUES ('Set n.2', 'Q400', 11);

-- ----------------------------
-- Table structure for t_contents_q410
-- ----------------------------
DROP TABLE IF EXISTS `t_contents_q410`;
CREATE TABLE `t_contents_q410`  (
  `contents` varchar(255) DEFAULT NULL,
  `sUnity` varchar(50) DEFAULT NULL,
  `sID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`sID`) USING BTREE,
  UNIQUE INDEX `contents`(`contents`, `sUnity`) USING BTREE
) ENGINE = InnoDB;

-- ----------------------------
-- Records of t_contents_q410
-- ----------------------------
INSERT INTO `t_contents_q410` VALUES ('- Par 1.1', 'Q410', 1);
INSERT INTO `t_contents_q410` VALUES ('- Par 1.2', 'Q410', 2);
INSERT INTO `t_contents_q410` VALUES ('- Par 2.1', 'Q410', 3);
INSERT INTO `t_contents_q410` VALUES ('- Par 2.2', 'Q410', 4);
INSERT INTO `t_contents_q410` VALUES ('- Par 2.3', 'Q410', 5);
INSERT INTO `t_contents_q410` VALUES ('<b>bold text</b>', 'Q410', 6);
INSERT INTO `t_contents_q410` VALUES ('<i>italic text</i>', 'Q410', 7);
INSERT INTO `t_contents_q410` VALUES ('<u>underline text</u>', 'Q410', 8);
INSERT INTO `t_contents_q410` VALUES ('normal text', 'Q410', 9);
INSERT INTO `t_contents_q410` VALUES ('Set n.1', 'Q410', 10);
INSERT INTO `t_contents_q410` VALUES ('Set n.2', 'Q410', 11);
——----------------------------
--t_目录的表结构\u q400
-- ----------------------------
删除表格(如果存在)`t_contents_q400`;
创建表't_contents_q400`(
`contents`varchar(255)默认为空,
`sUnity`varchar(50)默认为空,
`sID`int(11)非空自动增量,
使用BTREE的主键(`sID`),
使用BTREE的唯一索引'contents'('contents','sUnity`)
)引擎=InnoDB;
-- ----------------------------
--t_目录记录\u q400
-- ----------------------------
插入“Ti CordssQ400”值(“-PAR 1.1”、“Q400”、1);
插入“Ti CordssQ400”值(“-PAR 1.2”、“Q400”、2);
插入“Ti CordssQ400”值(“-PAR 2.1”、“Q400”、3);
插入“Ti CordssQ400”值(“-PAR 2.2”、“Q400”、4);
插入“Ti CordssQ400”值(“-PAR 2.3”、“Q400”、5);
在't_contents_q400'中插入值('bold text','q400',6);
在't_contents_q400'中插入值('斜体文本','q400',7);
在't_contents_q400'中插入值('下划线文本','q400',8);
在't_contents_q400'值中插入('normal text','q400',9);
在't_contents_q400'值中插入('Set n.1','q400',10);
在't_contents_q400'中插入值('Set n.2','q400',11);
-- ----------------------------
--t_目录的表结构\u q410
-- ----------------------------
删除表格(如果存在)`t_contents_q410`;
创建表't_contents_q410`(
`contents`varchar(255)默认为空,
`sUnity`varchar(50)默认为空,
`sID`int(11)非空自动增量,
使用BTREE的主键(`sID`),
使用BTREE的唯一索引'contents'('contents','sUnity`)
)引擎=InnoDB;
-- ----------------------------
--t_目录记录\u q410
-- ----------------------------
插入“Ti CordssQ410”值(“-PAR 1.1”、“Q410”、1);
插入“Ti CordssQ410”值(“-PAR 1.2”、“Q410”、2);
插入“Ti CordssQ410”值(“-PAR 2.1”、“Q410”、3);
插入“Ti CordssQ410”值(“-PAR 2.2”、“Q410”、4);
插入“Ti CordssQ410”值(“-PAR 2.3”、“Q410”、5);
在't_contents_q410'中插入值('bold text','q410',6);
在't_contents_q410'中插入值('斜体文本','q410',7);
在't_contents_q410'中插入值('下划线文本','q410',8);
在't_contents_q410'中插入值('normal text','q410',9);
在't_contents_q410'中插入值('Set n.1','q410',10);
在't_contents_q410'中插入值('Set n.2','q410',11);

在MySQL 8.0+中,您可以在每个表中使用
ROW_NUMBER()
窗口函数来获取行号并在该行上连接表:

WITH 
  cte1 AS (SELECT *, ROW_NUMBER() OVER (ORDER BY sID) rn FROM t_contents_q400),
  cte2 AS (SELECT *, ROW_NUMBER() OVER (ORDER BY sID) rn FROM t_contents_q410)
SELECT c1.contents Q400, c2.contents Q410
FROM cte1 c1 INNER JOIN cte2 c2
ON c2.rn = c1.rn
我假设这两个表的行数相同,就像您的示例数据一样。


请参阅。

您确定这两个表的行数始终相同吗?@GordonLinoff我不确定原因。您接受的答案假设它们是这样的——或者返回较小表中的行数。我不确定这是否是你想要的。@GordonLinoff我想要通过行数独立地合并所有表。。。可能是要联接的表之间的行数不同,但所有行都必须存在于return中,然后您应该检查您接受的任何行是否真的符合您的要求。我想没有。
-- ----------------------------
-- Table structure for t_contents_q400
-- ----------------------------
DROP TABLE IF EXISTS `t_contents_q400`;
CREATE TABLE `t_contents_q400`  (
  `contents` varchar(255) DEFAULT NULL,
  `sUnity` varchar(50) DEFAULT NULL,
  `sID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`sID`) USING BTREE,
  UNIQUE INDEX `contents`(`contents`, `sUnity`) USING BTREE
) ENGINE = InnoDB;

-- ----------------------------
-- Records of t_contents_q400
-- ----------------------------
INSERT INTO `t_contents_q400` VALUES ('- Par 1.1', 'Q400', 1);
INSERT INTO `t_contents_q400` VALUES ('- Par 1.2', 'Q400', 2);
INSERT INTO `t_contents_q400` VALUES ('- Par 2.1', 'Q400', 3);
INSERT INTO `t_contents_q400` VALUES ('- Par 2.2', 'Q400', 4);
INSERT INTO `t_contents_q400` VALUES ('- Par 2.3', 'Q400', 5);
INSERT INTO `t_contents_q400` VALUES ('<b>bold text</b>', 'Q400', 6);
INSERT INTO `t_contents_q400` VALUES ('<i>italic text</i>', 'Q400', 7);
INSERT INTO `t_contents_q400` VALUES ('<u>underline text</u>', 'Q400', 8);
INSERT INTO `t_contents_q400` VALUES ('normal text', 'Q400', 9);
INSERT INTO `t_contents_q400` VALUES ('Set n.1', 'Q400', 10);
INSERT INTO `t_contents_q400` VALUES ('Set n.2', 'Q400', 11);

-- ----------------------------
-- Table structure for t_contents_q410
-- ----------------------------
DROP TABLE IF EXISTS `t_contents_q410`;
CREATE TABLE `t_contents_q410`  (
  `contents` varchar(255) DEFAULT NULL,
  `sUnity` varchar(50) DEFAULT NULL,
  `sID` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`sID`) USING BTREE,
  UNIQUE INDEX `contents`(`contents`, `sUnity`) USING BTREE
) ENGINE = InnoDB;

-- ----------------------------
-- Records of t_contents_q410
-- ----------------------------
INSERT INTO `t_contents_q410` VALUES ('- Par 1.1', 'Q410', 1);
INSERT INTO `t_contents_q410` VALUES ('- Par 1.2', 'Q410', 2);
INSERT INTO `t_contents_q410` VALUES ('- Par 2.1', 'Q410', 3);
INSERT INTO `t_contents_q410` VALUES ('- Par 2.2', 'Q410', 4);
INSERT INTO `t_contents_q410` VALUES ('- Par 2.3', 'Q410', 5);
INSERT INTO `t_contents_q410` VALUES ('<b>bold text</b>', 'Q410', 6);
INSERT INTO `t_contents_q410` VALUES ('<i>italic text</i>', 'Q410', 7);
INSERT INTO `t_contents_q410` VALUES ('<u>underline text</u>', 'Q410', 8);
INSERT INTO `t_contents_q410` VALUES ('normal text', 'Q410', 9);
INSERT INTO `t_contents_q410` VALUES ('Set n.1', 'Q410', 10);
INSERT INTO `t_contents_q410` VALUES ('Set n.2', 'Q410', 11);
WITH 
  cte1 AS (SELECT *, ROW_NUMBER() OVER (ORDER BY sID) rn FROM t_contents_q400),
  cte2 AS (SELECT *, ROW_NUMBER() OVER (ORDER BY sID) rn FROM t_contents_q410)
SELECT c1.contents Q400, c2.contents Q410
FROM cte1 c1 INNER JOIN cte2 c2
ON c2.rn = c1.rn