3

我的任务是制作一个包含数千条记录的数千个其他表的汇总表。汇总表应包含:

  1. “记录”的非冗余列表;
  2. 包含该“记录”的表数
  3. 包含该“记录”的表的名称

示例是:

Table_1
nameA
nameB
nameC

Table_2
nameB
nameC
nameD

Table_3
nameC
nameD
nameE

结果应该是这样的

Record_name  Number  Table_names
nameA        1       Table_1
nameB        2       Table_1, Table_2
nameC        3       Table_1, Table_2, Table_3
nameD        2       Table_2, Table_3
nameE        1       Table_3

也许这不是一件复杂的事情,但是,由于我是新手mysql,我希望得到你们的一些建议和帮助!

4

1 回答 1

3
SELECT  col1 as `Record Number`, 
        COUNT(*) as `Number`, 
        GROUP_CONCAT(_name) as `table name`
FROM
(
    SELECT col1, 'table_1' as _name FROM table_1
    UNION ALL
    SELECT col1, 'table_2' as _name FROM table_2
    UNION ALL
    SELECT col1, 'table_3' as _name FROM table_3
) a
GROUP BY col1
于 2013-01-24T09:34:50.277 回答