SQL并排查询。 匹配路线

我试图写一个查询或两个,我可以用来并排显示同一个表的两个单独的视图。 我有logging与orig,dest和东西。 对于每个原始文件,每个目标都有一个logging,并且与其相关的内容。 所以说一个是亚特兰大,一个是凤凰。 亚特兰大和菲尼克斯将会有两个logging,分别是原始的或者是目的的:

ORIG / DEST / STUFF Atlanta / Phoenix / Stuff for this record Phoneix / Atlanta / Stuff for this record 

我需要的是亚特兰大并排列出所有的目标,而每一个目标与亚特兰大在另一端。 我需要为每个logging做这个。 所以结果会是这样的:

 Atlanta / Athens / Stuff : Athens / Atlanta / Stuff Atlanta / Chicago / Stuff : Chicago / Atlanta / Stuff Boulder / Athens / Stuff : Athens / Boulder / Stuff 

它需要最终在Excel中,所以我可以做两个单独的查询,但我不知道如何匹配它们。 这是我所得到的:

 SELECT a.orig, a.dest, a.stuff from table a WHERE a.orig = 'ATL' and a.orig IN (SELECT b.dest from table b WHERE b.dest = 'ATL') 

编辑:loggingATL / DEN和DEN / ATL之间的东西是不同的。

 Select a.orig, a.dest, a.stuff, b.orig, b.dest, b.stuff from a inner join a as b on a.orig = b.dest and b.orig = a.dest where a.orig = 'ATL' Order by a.orig, a.dest 

既然看来你要把凤凰与凤凰等排在一起,而且现在的东西是不一样的,有意义的

下面回答你在ATL中间的每一个组合的问题:

 select aleft.orig, aleft.dest, aleft.stuff, a.orig, a.dest, a.stuff from table a join table aleft on a.orig = aleft.dest where a.orig = 'ATL' 

试试这个:

 SELECT a.orig, a.dest, a.stuff, b.orig, b.dest, b.stuff FROM table a , table b WHERE a.orig = b.dest AND b.orig = a.dest ORDER BY a.orig, b.orig 

如果你需要每一个与ATL的组合作为dest或origin,只要做一个CROSS JOIN表b作为b SELECT a.orig,a.dest,a.stuff,b.orig,b.dest,b.stuff FROM table a WHERE a .orig ='ATL'或b.dest ='ATL'

你的例子有a.orig约束,所以如果你的意思是在你的问题中指出dest。