我有下表routes
:
from | to
---------
abc | cde
cde | abc
klm | xyz
xyz | klm
def | ghi
ghi | mno
mno | ghi
ghi | def
然后我提取每对独特的路线(在我的项目 abc -> cde = cde -> abc 中):
SELECT DISTINCT LEAST(from,to) AS point_a, GREATEST(from,to) AS point_B FROM routes
我最终得到以下结果:
point_a | point_b
-----------------
abc | cde
klm | xyz
def | ghi
ghi | mno
另外我有下表location
code | description
------------------
abc | home
cde | beach
ghi | work
xyz | club
klm | friend
...
我想将此表加入到上面的结果中,以便最终得到以下结果:
point_a | point_b | a_description | b_description
-------------------------------------------------
abc | cde | home | beach
klm | xyz | friend | club
...
什么查询可以一次完成所有这些?
我试图从中选择唯一的对routes
然后加入表格location
,或者location
先加入表格然后整理重复项,我要么得到错误,要么出现重复项......