Return 来自多 SELECT 子查询的多行
Return multiple rows from multi SELECT subquery
查询
SELECT
(SELECT NAME FROM product_component) AS pcNAME,
(SELECT PROJECT_NAME FROM jira_project) AS jpNAME,
(SELECT FILTER_NAME FROM jira_filter) AS jfNAME
每个 SELECT 将 return 行数不确定。我收到错误 Subquery returns more than 1 row
。我想要的输出将是这样的(速写):
=======================================
| pcNAME | jpNAME | jfNAME |
=======================================
| data | data | data |
+------------+------------+-----------+
| data | data | data |
+------------+------------+-----------+
| data | data | data |
+------------+------------+-----------+
| | data | data |
+------------+------------+-----------+
| | data | data |
+------------+------------+-----------+
| | data | |
+------------+------------+-----------+
每一列可能会产生与其他列不同的行数。所以我想从最大值中生成行数,然后清空其他未填充最大行数的行数。
注意: None 这些表有一个共享列,因此无法像 INNER JOIN
那样实现
关于如何实现这一点有什么想法吗?
在 MySQL 中处理此问题的一种方法是使用变量、union all
和聚合:
SELECT MAX(NAME) as NAME, MAX(PROJECT_NAME) as PROJECT_NAME,
MAX(FILTER_NAME) as FILTER_NAME
FROM ((SELECT (@rnpc := @rnpc + 1) as rn, NAME, NULL as PROJECT_NAME, NULL as FILTER_NAME
FROM product_component CROSS JOIN
(SELECT @rnpc := 0) params
) UNION ALL
(SELECT (@rnpn := @rnpn + 1) as rn, NULL, PROJECT_NAME, NULL as FILTER_NAME
FROM jira_project CROSS JOIN
(SELECT @rnpn := 0) params
) UNION ALL
(SELECT (@rnf := @rnf + 1) as rn, NAME, NULL as PROJECT_NAME, NULL as FILTER_NAME
FROM jira_filter CROSS JOIN
(SELECT @rnf := 0) params
)
) t
GROUP BY rn
ORDER BY rn;
查询
SELECT
(SELECT NAME FROM product_component) AS pcNAME,
(SELECT PROJECT_NAME FROM jira_project) AS jpNAME,
(SELECT FILTER_NAME FROM jira_filter) AS jfNAME
每个 SELECT 将 return 行数不确定。我收到错误 Subquery returns more than 1 row
。我想要的输出将是这样的(速写):
=======================================
| pcNAME | jpNAME | jfNAME |
=======================================
| data | data | data |
+------------+------------+-----------+
| data | data | data |
+------------+------------+-----------+
| data | data | data |
+------------+------------+-----------+
| | data | data |
+------------+------------+-----------+
| | data | data |
+------------+------------+-----------+
| | data | |
+------------+------------+-----------+
每一列可能会产生与其他列不同的行数。所以我想从最大值中生成行数,然后清空其他未填充最大行数的行数。
注意: None 这些表有一个共享列,因此无法像 INNER JOIN
关于如何实现这一点有什么想法吗?
在 MySQL 中处理此问题的一种方法是使用变量、union all
和聚合:
SELECT MAX(NAME) as NAME, MAX(PROJECT_NAME) as PROJECT_NAME,
MAX(FILTER_NAME) as FILTER_NAME
FROM ((SELECT (@rnpc := @rnpc + 1) as rn, NAME, NULL as PROJECT_NAME, NULL as FILTER_NAME
FROM product_component CROSS JOIN
(SELECT @rnpc := 0) params
) UNION ALL
(SELECT (@rnpn := @rnpn + 1) as rn, NULL, PROJECT_NAME, NULL as FILTER_NAME
FROM jira_project CROSS JOIN
(SELECT @rnpn := 0) params
) UNION ALL
(SELECT (@rnf := @rnf + 1) as rn, NAME, NULL as PROJECT_NAME, NULL as FILTER_NAME
FROM jira_filter CROSS JOIN
(SELECT @rnf := 0) params
)
) t
GROUP BY rn
ORDER BY rn;