| 您的当前位置:首页 --> MYSQL教程 --> MySQL中union和join语句使用区别的辨析教程 |
| MYSQL教程 MySQL中union和join语句使用区别的辨析教程 |
| 浏览次数:2138 关键词 ( ) |
| 查看使用该CPU的产品 查看CPU天梯 |
| CPU型号:MySQL中union和join语句使用区别的辨析教程 |
| 主频:Ghz |
| 睿频:Ghz |
| 核心数:个 |
| 不支持超核心 |
| 制作工艺: |
| 插槽类型: |
| 功耗:0W |
| L3缓存:0MB |
| 支持最大内存: 0GB |
| CPU详细参数 |
|
union和join是需要联合多张表时常见的关联词,具体概念我就不说了,想知道上网查就行,因为我也记不准确。 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,但是一定要写,不写会出错。 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之间的差别说过很多次,网上也有更详细的解释,不再赘述。 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数据库表
Table2数据库表
Table1表和Table2表具有相同的列结构,因此可以使用UNION运算符连接两个表的记录集,得到的连接结果如下表所示。 使用UNION连接Table3表和Table4表的记录
上述连接过程的实现代码可表示如下: SELECT * FROM Table1 UNION SELECT * FROM Table2 |
| 下一个产品 SQL计算timestamp的差值的方法 上一个产品 在阿里云的CentOS环境中安装配置MySQL的教程 |