你的位置:首页 > 数据库

[数据库]两表合并查询

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...............