gpt4 book ai didi

MYSQL - 将 UNION ALL 的 GROUP_CONCAT 结果合并为一行

转载 作者:行者123 更新时间:2023-11-29 05:18:19 25 4
gpt4 key购买 nike

我有以下声明:

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(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

关于MYSQL - 将 UNION ALL 的 GROUP_CONCAT 结果合并为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com