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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM