MYSQL - GROUP_CONCAT results of UNION ALL into one row

Ian2thedv

I have the following statement:

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";

Which produces the following results:

+---------------------------------+
| CONCAT("table1.", column_name)  |
+---------------------------------+
| table1.column1                  |
| table1.column2                  |
| table1.column3                  |
| table2.column4                  |
| table2.column5                  |
| table2.column6                  |
| table2.column7                  |
+---------------------------------+

I would like it to be in the following format:

+-----------------------------------------------------------------------------------------------------------+
| CONCAT("table1.", column_name)                                                                            |
+-----------------------------------------------------------------------------------------------------------+
| table1.column1,table1.column2,table1.column3,table2.column4,table2.column5,table2.column6,table2.column7  |
+-----------------------------------------------------------------------------------------------------------+

I have tried using GROUP_CONCAT like this:

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";

But this incorrectly produced the following results:

+--------------------------------------------------------------+
| CONCAT("table1.", column_name)                               |
+--------------------------------------------------------------+
| table1.column1,table1.column2,table1.column3                 |
| table2.column4,table2.column5,table2.column6,table2.column7  | 
+--------------------------------------------------------------+

From this I naturally tried to do a GROUP_CONCAT it as a sub query, like so:

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;

But there is a syntax error in the above statement. How would I concatenate the results of UNION ALL into one row?

I have reviewed the following questions without any success:

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

This is kind of related to my previous question: MySQL - Subquery in SELECT clause

murison

You were almost there, try this:

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;

to be honest - what dou you need this UNION for ? This would do the same...

SELECT  
  group_concat(distinct concat (table_name, '.', column_name) )
from columns 
WHERE table_name in ('table1', 'table2');

check the fiddle: http://sqlfiddle.com/#!9/d1172

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL - count and group by - display all results in one row

From Dev

MySQL - count and group by - display all results in one row

From Dev

mysql group_concat returns null row if one entry in the group is null

From Dev

Displaying Mysql Group_Concat results with Bootstrap

From Dev

MySQL LIKE from JOIN clause and GROUP_CONCAT returns only one row from joined table

From Dev

MySQL GROUP_CONCAT() groups all rows

From Dev

MySQL group by - union all

From Dev

GROUP_CONCAT returns 1 row for 0 results

From Dev

Double results in GROUP_CONCAT with double JOIN in MySQL

From Dev

Mysql group_concat with distinct and where gives strange results

From Dev

PHP MySQL Treat GROUP_CONCAT results as separate items

From Dev

MYSQL get count of search results with group_concat

From Dev

Query using group_concat is returning only one row

From Dev

Query using group_concat is returning only one row

From Dev

group_concat mysql subquery only returns one item

From Dev

MySQL GROUP_CONCAT and flip two rows in one

From Dev

group_concat does not show all the values mysql

From Dev

MySQL GROUP_CONCAT having all values same

From Dev

how do i group rows by an id row in group_concat into one row string?

From Dev

MySQL Group_Concat Not In

From Dev

Retrieve all the group records if at least one row matches the condition in MySQL

From Dev

MySQL one row of sum after limited results

From Dev

Count group_concat results

From Dev

create separate hyperlinks for group_concat and display the results into one table cell

From Dev

How to COUNT MySQL results when I use “GROUP_CONCAT” and “HAVING” in the query?

From Dev

MySQL Query error using UNION/UNION ALL and Group By

From Dev

If condition with group_concat in mysql

From Dev

Mysql with GROUP_CONCAT in subselect

From Dev

Mysql GROUP_CONCAT and IN query

Related Related

  1. 1

    MySQL - count and group by - display all results in one row

  2. 2

    MySQL - count and group by - display all results in one row

  3. 3

    mysql group_concat returns null row if one entry in the group is null

  4. 4

    Displaying Mysql Group_Concat results with Bootstrap

  5. 5

    MySQL LIKE from JOIN clause and GROUP_CONCAT returns only one row from joined table

  6. 6

    MySQL GROUP_CONCAT() groups all rows

  7. 7

    MySQL group by - union all

  8. 8

    GROUP_CONCAT returns 1 row for 0 results

  9. 9

    Double results in GROUP_CONCAT with double JOIN in MySQL

  10. 10

    Mysql group_concat with distinct and where gives strange results

  11. 11

    PHP MySQL Treat GROUP_CONCAT results as separate items

  12. 12

    MYSQL get count of search results with group_concat

  13. 13

    Query using group_concat is returning only one row

  14. 14

    Query using group_concat is returning only one row

  15. 15

    group_concat mysql subquery only returns one item

  16. 16

    MySQL GROUP_CONCAT and flip two rows in one

  17. 17

    group_concat does not show all the values mysql

  18. 18

    MySQL GROUP_CONCAT having all values same

  19. 19

    how do i group rows by an id row in group_concat into one row string?

  20. 20

    MySQL Group_Concat Not In

  21. 21

    Retrieve all the group records if at least one row matches the condition in MySQL

  22. 22

    MySQL one row of sum after limited results

  23. 23

    Count group_concat results

  24. 24

    create separate hyperlinks for group_concat and display the results into one table cell

  25. 25

    How to COUNT MySQL results when I use “GROUP_CONCAT” and “HAVING” in the query?

  26. 26

    MySQL Query error using UNION/UNION ALL and Group By

  27. 27

    If condition with group_concat in mysql

  28. 28

    Mysql with GROUP_CONCAT in subselect

  29. 29

    Mysql GROUP_CONCAT and IN query

HotTag

Archive