mysql 内连接条件_Mysql内连接有OR条件?

我有两张桌子,如下图

location_distance

----------------------------------------------

id | fromLocid | toLocid | distance

----------------------------------------------

1 | 3 | 5 | 70

2 | 6 | 8 | 15

3 | 2 | 4 | 63

...other_table

--------------------------------------------

Id | fromLocid | toLocid | otherdata

--------------------------------------------

12 | 5 | 3 | xxxx

22 | 2 | 4 | xxxx

56 | 8 | 6 | xxxx

78 | 3 | 5 | xxxx我想检索每行的other_table中位置的距离。这是我试过的

SELECT ot.*, ld.distance FROM other_table AS ot

INNER JOIN location_distance ld ON ld.fromLocid = ot.fromLocid AND ld.toLocid = ot.toLocid如果位置值反过来,这不会返回行。我怎样才能重写上述查询产生预期的结果?我应该在联合条款中引入OR条件吗?像下面一样?

SELECT ot.*, ld.distance FROM other_table AS ot

INNER JOIN location_distance ld ON (ld.fromLocid = ot.fromLocid OR ld.fromLocid = ot.toLocid) AND (ld.toLocid = ot.fromLocid OR ld.toLocid = ot.fromLocid)但这个查询解释说:“范围检查每个记录”。 ..这是一个不好的做法?

结果

--------------------------------------------------------

Id | fromLocid | toLocid | otherdata | distance

--------------------------------------------------------

22 | 2 | 4 | xxxx | 63

78 | 3 | 5 | xxxx | 70预期结果应该是

-----------------------------------------------------

Id | fromLocid | toLocid | otherdata | distance

-----------------------------------------------------

12 | 5 | 3 | xxxx | 70

22 | 2 | 4 | xxxx | 63

56 | 8 | 6 | xxxx | 15

78 | 3 | 5 | xxxx | 70

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值