mysql——多表——合并查询结果

Posted 小白龙白龙马

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql——多表——合并查询结果相关的知识,希望对你有一定的参考价值。

合并查询结果

合并查询结果 是将多个select语句的查询结果合并到一起

union关键字,数据库会将所有的查询结果合并到一起,然后除掉相同的记录;

union all关键字,只是简单的合并到一起

 

前期表准备:

create table employee ( num int(50),
                        d_id int(50),
                        name varchar(50),
                        age int(50),
                        sex varchar(50),
                        homeadd varchar(50)
                       );

insert into employee values(1,1001,\'zhangsan\',26,\'nan\',\'beijing\');
insert into employee values(2,1001,\'lisi\',24,\'nv\',\'hunan\');
insert into employee values(3,1002,\'wangwu\',25,\'nan\',\'jiangsu\');
insert into employee values(4,1004,\'aric\',15,\'nan\',\'yingguo\');

select * from employee;

create table department ( d_id int(50),
                          d_name varchar(50),
                          functione varchar(50),
                          address varchar(50)
                        );

insert into department values(1001,\'keyanbu\',\'yanfachanpin\',\'3lou5hao\');
insert into department values(1002,\'shengchanbu\',\'shengchanchanp\',\'5louyiceng\');
insert into department values(1003,\'xiaoshoubu\',\'cehuaxiaoshou\',\'1louxiaoshoudating\');

select * from department;

 

select d_id from department union select d_id from employee;

 

 

 

 

 

 

select d_id from department union all select d_id from employee;

以上是关于mysql——多表——合并查询结果的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL-多表查询

day15(mysql 的多表查询,事务)

mysql多表查询合并到一个临时表,怎么再加一列并把各自的表名加上?

MySQL 多表查询

MySQL进阶4

mysql多表查询