当前位置: 代码迷 >> MySQL >> 两表归并查询
  详细解决方案

两表归并查询

热度:127   发布时间:2016-05-05 16:54:26.0
两表合并查询

UNION 

UNION ALL

//Table Attribute 

Create table student(  

  id int unsigned not null auto_increment,

  name varchar(20) not null,

  primary key(id)

)ENGINE=InnoDb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ;

Create table student1(  

  id int unsigned not null auto_increment,

  name varchar(20) not null,

  primary key(id)

)ENGINE=InnoDb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ;

 

select id name from student

UNION ALL

select 0,name from student1

Output:

1     小明      

2  Jack  --->student

0  Tom     --->student1

0  Jack  --->student

 

select id name from student

UNION ALL

select id,name from student1

Output:

1     小明      

2  Jack  --->student

1  Tom     --->student1

2  Jack  --->student

 

select id name from student

UNION               ----<UNION>----

select id,name from student1

Output:

1     小明      

2  Jack  --->student

1  Tom     --->student1

Jack  is missing...............

  相关解决方案