MYSQL - GROUP_CONCAT 将 UNION ALL 的结果合并为一行
MYSQL - GROUP_CONCAT results of UNION ALL into one row
我有以下声明:
SELECT DISTINCT Concat("table1.", column_name)
FROM information_schema.columns
WHERE table_name = "table1"
UNION ALL
SELECT DISTINCT Concat("table2.", column_name)
FROM information_schema.columns
WHERE table_name = "table2";
产生以下结果:
+---------------------------------+
| CONCAT("table1.", column_name) |
+---------------------------------+
| table1.column1 |
| table1.column2 |
| table1.column3 |
| table2.column4 |
| table2.column5 |
| table2.column6 |
| table2.column7 |
+---------------------------------+
我希望它采用以下格式:
+-----------------------------------------------------------------------------------------------------------+
| CONCAT("table1.", column_name) |
+-----------------------------------------------------------------------------------------------------------+
| table1.column1,table1.column2,table1.column3,table2.column4,table2.column5,table2.column6,table2.column7 |
+-----------------------------------------------------------------------------------------------------------+
我试过这样使用 GROUP_CONCAT
:
SELECT Group_Concat(DISTINCT Concat("table1.", column_name))
FROM information_schema.columns
WHERE table_name = "table1"
UNION ALL
SELECT Group_Concat(DISTINCT Concat("table2.", column_name))
FROM information_schema.columns
WHERE table_name = "table2";
但这错误地产生了以下结果:
+--------------------------------------------------------------+
| CONCAT("table1.", column_name) |
+--------------------------------------------------------------+
| table1.column1,table1.column2,table1.column3 |
| table2.column4,table2.column5,table2.column6,table2.column7 |
+--------------------------------------------------------------+
从这里我很自然地尝试做一个 GROUP_CONCAT
它作为一个子查询,像这样:
SELECT GROUP_Concat(
SELECT Group_Concat(DISTINCT Concat("table1.", column_name))
FROM information_schema.columns
WHERE table_name = "table1"
UNION ALL
SELECT Group_Concat(DISTINCT Concat("table2.", column_name))
FROM information_schema.columns
WHERE table_name = "table2" t)
FROM t;
但是上面的语句存在语法错误。我如何将 UNION ALL
的结果连接成一行?
我已经复习了以下问题但没有成功:
Combing results from union all into one row when some columns have different values
How do I combine two queries (union all) into one row?
MySQL COUNT results of UNION ALL statement
这与我之前的问题有点相关:
MySQL - Subquery in SELECT clause
SELECT GROUP_Concat(a)
FROM (
SELECT Group_Concat(DISTINCT Concat("table1.", column_name)) as a
FROM information_schema.columns
WHERE table_name = "table1"
UNION ALL
SELECT Group_Concat(DISTINCT Concat("table2.", column_name))
FROM information_schema.columns
WHERE table_name = "table2" ) t
GROUP BY 1
你快到了,试试这个:
select group_concat(xxx) from (
SELECT DISTINCT Concat("table1.", column_name) as xxx
FROM columns
WHERE table_name = "table1"
UNION ALL
SELECT DISTINCT Concat("table2.", column_name)
FROM columns
WHERE table_name = "table2"
) as src;
老实说 - 你需要这个 UNION
做什么?这会做同样的事情...
SELECT
group_concat(distinct concat (table_name, '.', column_name) )
from columns
WHERE table_name in ('table1', 'table2');
检查 fiddle: http://sqlfiddle.com/#!9/d1172
我有以下声明:
SELECT DISTINCT Concat("table1.", column_name)
FROM information_schema.columns
WHERE table_name = "table1"
UNION ALL
SELECT DISTINCT Concat("table2.", column_name)
FROM information_schema.columns
WHERE table_name = "table2";
产生以下结果:
+---------------------------------+
| CONCAT("table1.", column_name) |
+---------------------------------+
| table1.column1 |
| table1.column2 |
| table1.column3 |
| table2.column4 |
| table2.column5 |
| table2.column6 |
| table2.column7 |
+---------------------------------+
我希望它采用以下格式:
+-----------------------------------------------------------------------------------------------------------+
| CONCAT("table1.", column_name) |
+-----------------------------------------------------------------------------------------------------------+
| table1.column1,table1.column2,table1.column3,table2.column4,table2.column5,table2.column6,table2.column7 |
+-----------------------------------------------------------------------------------------------------------+
我试过这样使用 GROUP_CONCAT
:
SELECT Group_Concat(DISTINCT Concat("table1.", column_name))
FROM information_schema.columns
WHERE table_name = "table1"
UNION ALL
SELECT Group_Concat(DISTINCT Concat("table2.", column_name))
FROM information_schema.columns
WHERE table_name = "table2";
但这错误地产生了以下结果:
+--------------------------------------------------------------+
| CONCAT("table1.", column_name) |
+--------------------------------------------------------------+
| table1.column1,table1.column2,table1.column3 |
| table2.column4,table2.column5,table2.column6,table2.column7 |
+--------------------------------------------------------------+
从这里我很自然地尝试做一个 GROUP_CONCAT
它作为一个子查询,像这样:
SELECT GROUP_Concat(
SELECT Group_Concat(DISTINCT Concat("table1.", column_name))
FROM information_schema.columns
WHERE table_name = "table1"
UNION ALL
SELECT Group_Concat(DISTINCT Concat("table2.", column_name))
FROM information_schema.columns
WHERE table_name = "table2" t)
FROM t;
但是上面的语句存在语法错误。我如何将 UNION ALL
的结果连接成一行?
我已经复习了以下问题但没有成功:
Combing results from union all into one row when some columns have different values
How do I combine two queries (union all) into one row?
MySQL COUNT results of UNION ALL statement
这与我之前的问题有点相关: MySQL - Subquery in SELECT clause
SELECT GROUP_Concat(a)
FROM (
SELECT Group_Concat(DISTINCT Concat("table1.", column_name)) as a
FROM information_schema.columns
WHERE table_name = "table1"
UNION ALL
SELECT Group_Concat(DISTINCT Concat("table2.", column_name))
FROM information_schema.columns
WHERE table_name = "table2" ) t
GROUP BY 1
你快到了,试试这个:
select group_concat(xxx) from (
SELECT DISTINCT Concat("table1.", column_name) as xxx
FROM columns
WHERE table_name = "table1"
UNION ALL
SELECT DISTINCT Concat("table2.", column_name)
FROM columns
WHERE table_name = "table2"
) as src;
老实说 - 你需要这个 UNION
做什么?这会做同样的事情...
SELECT
group_concat(distinct concat (table_name, '.', column_name) )
from columns
WHERE table_name in ('table1', 'table2');
检查 fiddle: http://sqlfiddle.com/#!9/d1172