本人有两张表
表 A
C1 C2 C3
1 2 3
3 5 6
表 B
C1 C3 C4 C5
4 5 7 1
2 3 4 6
注:两张表的字段不同 并且完全独立 没有关系
最终理想结果:
不存在的表字段为 n
n = null
C1 C2 C3 C4 C5
1 2 3 n n
3 5 6 n n
4 n 5 7 1
2 n 3 4 6
表 A
C1 C2 C3
1 2 3
3 5 6
表 B
C1 C3 C4 C5
4 5 7 1
2 3 4 6
注:两张表的字段不同 并且完全独立 没有关系
最终理想结果:
不存在的表字段为 n
n = null
C1 C2 C3 C4 C5
1 2 3 n n
3 5 6 n n
4 n 5 7 1
2 n 3 4 6
解决方案
20
select C1 ,C2, C3, 0 as C4 ,0 as C5 from A
union all
select C1 ,0, C3, C4 , C5 from B
union all
select C1 ,0, C3, C4 , C5 from B
20
mysql> create table t1(c1 varchar(100),c2 varchar(100),c3 varchar(100));
Query OK, 0 rows affected (0.41 sec)
mysql> insert into t1 values ("1","2","3");
Query OK, 1 row affected (0.22 sec)
mysql> insert into t1 values ("3","5","6");
Query OK, 1 row affected (0.19 sec)
mysql> create table t2(c1 varchar(100),c3 varchar(100),c4 varchar(100),c5 varchar(100));
Query OK, 0 rows affected (0.33 sec)
mysql> insert into t2 values ("4","5","7","1");
Query OK, 1 row affected (0.17 sec)
mysql> insert into t2 values ("2","3","4","7");
Query OK, 1 row affected (0.15 sec)
mysql> select c1,c2,c3,null c4,null c5 from t1
-> union all
-> select c1,null c2,c3,c4,c5 from t2;
+--+--+--+--+--+
| c1 | c2 | c3 | c4 | c5 |
+--+--+--+--+--+
| 1 | 2 | 3 | NULL | NULL |
| 3 | 5 | 6 | NULL | NULL |
| 4 | NULL | 5 | 7 | 1 |
| 2 | NULL | 3 | 4 | 7 |
+--+--+--+--+--+
4 rows in set (0.00 sec)
mysql>