将多个表连接到单个mysql表中,为每个记录添加新字段

时间:2014-05-08 09:20:21

标签: mysql

我对mysql有疑问。我有多个表(德累斯顿,杜塞尔多夫,英格兰等),每个表都有以下字段(名称,登录名,macaddress)。有些用户在多个表中(同一个用户可以在两个或多个表中。)我想创建一个表,我可以看到所有用户。我想知道是否有可能在每个原始结尾处添加一个字段,告诉我来自哪里来并合并所有重复的文件。我希望自己清楚......我是mysql的新手......

最好的问候......

以下是我的表格:

杜塞尔多夫:

--------------------------------
|Username | value | macaddress |
-------------------------------
|jose     | 10    | 00:00:00:00|
--------------------------------
|maria    | 20    | 00:00:00:01|
--------------------------------

德累斯顿:

--------------------------------
|Username | value | macaddress |
--------------------------------
|jose     | 10    | 00:00:00:00|
--------------------------------
|maria    | 20    | 00:00:00:01|
--------------------------------
|juan     | 15    | 00:00:00:02|
--------------------------------

英国:

--------------------------------
|Username | value | macaddress |
-------------------------------
|jose     | 10    | 00:00:00:00|
-------------------------------
|maria    | 20    | 00:00:00:01|
--------------------------------
|pedro    | 20    | 00:00:00:03|
--------------------------------

所有在一起:

-----------------------------------------------------------------
|Username | value | macaddress | Dusseldorf | Dresden | England |
-----------------------------------------------------------------
|jose     | 10    | 00:00:00:00|     X      |    X    |    X    |
-----------------------------------------------------------------
|maria    | 20    | 00:00:00:01|     X      |    X    |    X    |
-----------------------------------------------------------------
|juan     | 15    | 00:00:00:02|            |    X    |         |
-----------------------------------------------------------------
|pedro    | 20    | 00:00:00:03|            |         |    X    |
-----------------------------------------------------------------

3 个答案:

答案 0 :(得分:0)

您可以使用多个UNION语句选择此数据,并使用CREATE TABLE创建新表:

CREATE TABLE all_merged AS
 SELECT s.*,
        concat((SELECT 'Dresden' 
                  FROM Dresden
                 WHERE Dresden.name=s.name
                   AND Dresden.loginname = s.loginname
                   AND Dresden.macaddress = s.macaddress
               ),
               (SELECT 'Dusseldorf' 
                  FROM Dusseldorf
                 WHERE Dresden.name=s.name
                   AND Dresden.loginname = s.loginname
                   AND Dresden.macaddress = s.macaddress
               ),
               ...) as `location`
   FROM
        ( SELECT name, loginname,macaddress
            FROM Dresden
           UNION
          SELECT name, loginname,macaddress
            FROM Dusseldorf
           UNION
           ....
        ) AS S
 )

我认为建立一个视图对你的情况更好:

CREATE VIEW v_all_merged AS
 SELECT s.*,
        concat((SELECT 'Dresden' 
                  FROM Dresden
                 WHERE Dresden.name=s.name
                   AND Dresden.loginname = s.loginname
                   AND Dresden.macaddress = s.macaddress
               ),
               (SELECT 'Dusseldorf' 
                  FROM Dusseldorf
                 WHERE Dresden.name=s.name
                   AND Dresden.loginname = s.loginname
                   AND Dresden.macaddress = s.macaddress
               ),
               ...) as `location`
   FROM
        ( SELECT name, loginname,macaddress
            FROM Dresden
           UNION
          SELECT name, loginname,macaddress
            FROM Dusseldorf
           UNION
           ....
        ) AS S
 )

我刚刚创建了一个sqlfiddle来展示解决方案

答案 1 :(得分:0)

这似乎就是你要找的东西。建立一个基于其他表的新表,并为原点添加一个额外的字段:

create table users as
select name, loginname, macaddress, 'Dresden' as origin from dresden
union all
select name, loginname, macaddress, 'Düsseldorf' as origin from dusseldorf
union all
select name, loginname, macaddress, 'England' as origin from england
...

当然,不是一次又一次地存储相同的原始字符串,而是应该有两个表(但使用相同的技术):

create table origins(id int, origin varchar(30));
insert into table origins (1, 'Dresden');
insert into table origins (2, 'Düsseldorf');
...

create table users as
select name, loginname, macaddress, 1 as id_origin from dresden
union all
select name, loginname, macaddress, 2 as id_origin from dusseldorf
...

答案 2 :(得分:0)

您可以创建一个包含连接位置字符串的表,如下所示。虽然我建议最好为用户,位置及其关系设置三个单独的表。

create table users as
select 
  loginname, 
  max(name) as name, 
  max(macaddress) as macaddress, 
  group_concat(location) as locations
from
(
  select name, loginname, macaddress, 'Dresden' as location from dresden
  union all
  select name, loginname, macaddress, 'Düsseldorf' as location from dusseldorf
  union all
  select name, loginname, macaddress, 'England' as location from england
  ...
);