我正在研究不同年份(2009年至2014年)的铁路数据,但最近又分别收到了两年(2015年和2016年)的数据。所以我目前的表格如下:
YEAR....REV1....REV2....REV3....REV4....ORG....DEST
2009.....12......14.....15......16......ABC.....ABD
2010.....12......14.....15......16......ABC.....ABD
2011.....12......14.....15......16......ABC.....ABD
2012.....12......14.....15......16......ABC.....ABD
2013.....12......14.....15......16......ABC.....ABD
2014.....12......14.....15......16......ABC.....ABD
2009.....12......14.....15......16......XYZ.....XCV
2010.....12......14.....15......16......XYZ.....XCV
2011.....12......14.....15......16......XYZ.....XCV
2012.....12......14.....15......16......XYZ.....XCV
2013.....12......14.....15......16......XYZ.....XCV
2014.....12......14.....15......16......XYZ.....XCV
请注意,值12,14,15,16只是我现在写的随机数,值不同,一年不同。并且列表继续为每个不同的ORG和DEST对提供行。
YEAR....REV1....REV2....REV3....REV4....ORG....DEST
2015.....12......14.....15......16......ABC.....ABD
2015.....12......14.....15......16......XYZ.....XCV
2015.....12......14.....15......16......VRF.....AVC
2015.....12......14.....15......16......DRE.....ACS
2015.....12......14.....15......16......FRE.....AWD
YEAR....REV1....REV2....REV3....REV4....ORG....DEST
2016.....12......14.....15......16......ABC.....ABD
2016.....12......14.....15......16......XYZ.....XCV
2016.....12......14.....15......16......VRF.....AVC
2016.....12......14.....15......16......DRE.....ACS
2016.....12......14.....15......16......FRE.....AWD
我想要的是一张包含所有年份的表格,但只添加ORG AND DEST基础上的表格,因为新表格(2015年和2016年)包含许多其他ORG对的数据DEST我不感兴趣,而只是我在第一张表中的那些,如下所示:
YEAR....REV1....REV2....REV3....REV4....ORG....DEST
2009.....12......14.....15......16......ABC.....ABD
2010.....12......14.....15......16......ABC.....ABD
2011.....12......14.....15......16......ABC.....ABD
2012.....12......14.....15......16......ABC.....ABD
2013.....12......14.....15......16......ABC.....ABD
2014.....12......14.....15......16......ABC.....ABD
2015 ....12......14.....15......16......ABC.....ABD
2016.....12......14.....15......16......ABC.....ABD
2009.....12......14.....15......16......XYZ.....XCV
2010.....12......14.....15......16......XYZ.....XCV
2011.....12......14.....15......16......XYZ.....XCV
2012.....12......14.....15......16......XYZ.....XCV
2013.....12......14.....15......16......XYZ.....XCV
2014.....12......14.....15......16......XYZ.....XCV
2015.....12......14.....15......16......XYZ.....XCV
2016.....12......14.....15......16......XYZ.....XCV
我对SQL比较陌生,所以一直在阅读内部联接,但它似乎添加了列而不是我想要的行。关于我如何做到这一点的任何想法?
提前谢谢。
答案 0 :(得分:0)
UNION会成功,尝试下面的代码
SELECT * FROM Table1
UNION
SELECT * FROM Table2 WHERE `ORG` LIKE 'XYZ' AND `DEST` LIKE 'XCV'
UNION
SELECT * FROM Table3 WHERE `ORG` LIKE 'XYZ' AND `DEST` LIKE 'XCV'
答案 1 :(得分:0)
+1为您的努力。 Join
适用于匹配2个不同的列。根据{{1}}
union
个不同的表格
where
如果您还想要重复数据,请使用 SELECT * FROM Table1
UNION SELECT * FROM Table2 WHERE `ORG` LIKE 'XYZ' AND `DEST` LIKE 'XCV'
UNION SELECT * FROM Table3 WHERE `ORG` LIKE 'XYZ' AND `DEST` LIKE 'XCV'
代替union。