搜索
首页数据库mysql教程MySQL中union和join语句使用区别的辨析教程_MySQL

union和join是需要联合多张表时常见的关联词,具体概念我就不说了,想知道上网查就行,因为我也记不准确。
先说差别:union对两张表的操作是合并数据条数,等于是纵向的,要求是两张表字段必须是相同的(Schema of both sides of union should match.)。也就是说如果A表中有三条数据,B表中有两条数据,那么A union B就会有五条数据。说明一下union 和union all的差别,对于union如果存在相同的数据记录会被合并,而union all不会合并相同的数据记录,该有多少条记录就会有多少条记录。例如在mysql下执行以下语句:


select * from tmp_libingxue_a;
name number
libingxue 1001
yuwen 1002
select * from tmp_libingxue_b;
name number
libingxue 1001
feiyao 1003
select * from tmp_libingxue_a union select * from tmp_libingxue_b;
libingxue 1001
yuwen 1002
feiyao 1003
select * from tmp_libingxue_a union all select * from tmp_libingxue_b;
libingxue 1001
yuwen 1002
libingxue 1001
feiyao 1003


但是这样在hive里面是不能执行的,执行select * from tmp_libingxue_a union all select * from tmp_libingxue_b;会failed,hive中union必须在子查询中进行。如


select * from (select * from tmp_yuwen_a union all select * from tmp_yuwen_b) t1;


注意,必须是union all,单独用union它会提示你缺少ALL,而且后面的t1必须写,你可以写成a或者b,但是一定要写,不写会出错。
而join则是偏于横向的联合,仅仅是偏向于,等下详细说明。join跟union比起来显得更宽松,对两个表的字段不做要求,没有限制条件的join等于两个表的笛卡尔乘积,所有join需要有限制条件来约束,经过限制的join就是横向的扩张了。对于满足限制条件的join会被提取出来,不满足的直接过滤掉。用法可以很灵活,下面有两个简单的例子:


select * from (select * from tmp_yuwen_a)t1 join (select * from tmp_yuwen_b) t2;
select * from tmp_yuwen_a t1 join (select * from tmp_yuwen_b) t2;


left outer join和right outer join用法类似,区别就是left outer join会把左边表的字段全部选择出来,右边表的字段把符合条件的也选择出来,不满足的全部置空,也就是说以左边表为参照。right outer join同理以右边表为参照。这三个join之间的差别说过很多次,网上也有更详细的解释,不再赘述。
相同点:在某些特定的情况下,可以用join实现union all的功能,这种情况是有条件的,当出现这种情况的时候选择union all还是group by就可以看情况或者看两者的消耗而决定。sql虽然就在那么几个关键词,但变化多端、功能强大,只要能实现想要的功能,怎么用随便你。需求情况sql简单重现如下


drop table tmp_libingxue_resource;
create external table if not exists tmp_libingxue_resource(
  user_id string,
  shop_id string,
  auction_id  string,
  search_time  string
)partitioned by (pt string)
row format delimited fields terminated by '\t'
lines terminated by '\n'
stored as sequencefile;

drop table tmp_libingxue_result;
create external table if not exists tmp_libingxue_result(
  user_id string,
  shop_id string,
  auction_id  string,
  search_time  string
)partitioned by (pt string)
row format delimited fields terminated by '\t'
lines terminated by '\n'
stored as sequencefile;

insert overwrite table tmp_libingxue_result where(pt=20041104) select * from tmp_libingxue_resource;



sudo -u taobao hadoop dfs -rmr /group/tbads/warehouse/tmp_libingxue_result/pt=20041104
sudo -u taobao hadoop jar /home/taobao/dataqa/framework/DailyReport.jar 
com.alimama.loganalyzer.tool.SeqFileLoader tmp_libingxue_resource.txt 
hdfs://v039182.sqa.cm4:54310/group/tbads/warehouse/tmp_libingxue_result/pt=20041104/part-00000



hive> select * from tmp_libingxue_resource;



OK
2001 0  11  101  20041104
2002 0  11  102  20041104



hive> select * from tmp_libingxue_result;



OK
2001 0  12  103  20041104
2002 0  12  104  20041104



select user_id,shop_id,max(auction_id),max(search_time)
from
(select * from tmp_libingxue_resource 
union all
select * from tmp_libingxue_result )t1
group by user_id,shop_id;



2001 0  12  103
2002 0  12  104



select t1.user_id,t1.shop_id,t2.auction_id,t2.search_time
from
(select * from tmp_libingxue_resource) t1
join
(select * from tmp_libingxue_result) t2
on t1.user_id=t2.user_id and t1.shop_id=t2.shop_id;



2001 0  12  103
2002 0  12  104



通过前面的介绍,使用UNION对表的结果集进行并运算与使用JOIN对多表进行连接,二者有本质的不同。
下面给出一个使用UNION运算符连接二表记录的运算实例。
典型的二表记录的UNION运算


假定有两个表Table3和Table4,其包含的列和数据分别如下所示。

Table1数据库表

20151216110017983.png (591×135)

Table2数据库表

20151216110102452.png (582×131)

Table1表和Table2表具有相同的列结构,因此可以使用UNION运算符连接两个表的记录集,得到的连接结果如下表所示。

使用UNION连接Table3表和Table4表的记录

20151216110125850.png (589×272)

上述连接过程的实现代码可表示如下:


SELECT *
FROM Table1
UNION
SELECT *
FROM Table2

以上就是MySQL中union和join语句使用区别的辨析教程_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具