2017-05-05 51 views
0

我有在MySQL 3个表==>表A,tableB的,表C将2- MySQL表

TableA中,我有以下

+---------+----+----+--------+------------+--+ 
| Name | N1 | N2 | Color | State | | 
+---------+----+----+--------+------------+--+ 
| John | 60 | 50 | Red | Newyork | | 
| Tom  | 70 | 60 | Green | Kansas  | | 
| Mathew | 50 | 40 | Blue | Texas  | | 
| James | 40 | 30 | Yellow | Texas  | | 
| SSS  | 70 | 60 | Pink | Washington | | 
+---------+----+----+--------+------------+--+ 

TableB中,我有以下

+--------+----+----+ 
| S.Name | N3 | N4 | 
+--------+----+----+ 
| Carl | 10 | 18 | 
| Jason | 15 | 9 | 
| Cindy | 13 | 12 | 
| Tim | 7 | 18 | 
| Pam | 15 | 14 | 
+--------+----+----+ 

表C,我想下面的

+---------+----+----+--------+------------+--------+ 
| Name | N1 | N2 | Color | State | S.Name | 
+---------+----+----+--------+------------+--------+ 
| John | 60 | 50 | Red | Newyork |  | 
| Tom  | 70 | 60 | Green | Kansas  |  | 
| Mathew | 50 | 40 | Blue | Texas  |  | 
| James | 40 | 30 | Yellow | Texas  |  | 
| SSS  | 70 | 60 | Pink | Washington |  | 
|   | 10 | 18 |  |   | Carl | 
|   | 15 | 9 |  |   | Jason | 
|   | 13 | 12 |  |   | Cindy | 
|   | 7 | 18 |  |   | Tim | 
|   | 15 | 14 |  |   | Pam | 
+---------+----+----+--------+------------+--------+ 

请帮我用这个sql查询。由于

+0

您应该使用这个视图。 –

+0

我可否知道你为什么要把它存放到第三张桌子? – Demonyowh

+0

@LuudvanKeulen为什么? – Strawberry

回答

1

有两种表作为提以上 当我们运行代码

Create table tablc as 
select name,n1,n2,color,State,' ' as sname FROM tablea 
union 
select '' as name,n3,n4,'' as color ,'' as State ,'s.name' as sname from tableb 

新表将创建enter image description here

输出将显示为表tablc

3

SELECT结果,你可以使用简单的union all

select Name as Name, N1 as N1, N2 as N2, Color as Color, State as State, null as S_Name from tableA 
union all 
select null as Name, N3 as N1, N4 as N2, null as Color, null as State, S_Name as S_Name from tableB 
+0

我跑了查询,但我可以看到数据库,但是当我刷新表时,数据消失......不知道发生了什么? –

+0

@ShahidShabbir - 这只是'select'查询,这不存储结果无处。你需要将结果存储到另一个表中? –

+0

谢谢,我想通了,这是一个需要插入语句。谢谢 –

1

你可以通过工会叫它什么都

select name, n1, n2, color, state, null as 's_name' as recordtype from tablea 
union all 
select null as 'name', n3 as 'n1', n4 as 'n2', null as 'color', null as 
'state', s_name from tableb 
1
create table tablc as 
select name,n1,n2,color,State,' ' as sname FROM tablea 
union 
select '' as name,n3,n4,'' as color ,'' as State ,'s.name' as sname from tableb 
+0

我运行了查询,然后我可以看到数据库,但是当我刷新表格时,数据消失了......不知道发生了什么? –