简体   繁体   English

Mysql 内连接与 OR 条件?

[英]Mysql Inner join with OR condition?

I have 2 tables like below我有 2 张如下表

location_distance 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

I would like to retrieve the distance b/w the locations in other_table for each row.我想为每一行检索 other_table 中位置的黑白距离。 Here's what i've tried这是我尝试过的

SELECT ot.*, ld.distance FROM other_table AS ot 
    INNER JOIN location_distance ld ON ld.fromLocid = ot.fromLocid AND ld.toLocid = ot.toLocid

This doesnt return the rows if the locations values are vice versa.如果位置值相反,这不会返回行。 How can i rewrite the above query to produce expected result?如何重写上述查询以产生预期结果? Should i inlude OR condition on the join clause?我应该在 join 子句中包含OR条件吗? like below?像下面?

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)

but this query Explain says "Range checked for each record".但是这个查询解释说“范围检查每条记录”。 .. is this a bad practise? ..这是一种不好的做法吗?

Result结果

--------------------------------------------------------
Id  | fromLocid | toLocid    | otherdata   | distance
--------------------------------------------------------
22  |  2        |   4        | xxxx        | 63
78  |  3        |   5        | xxxx        | 70

Expected Result should be预期结果应该是

-----------------------------------------------------
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

You can join on the location_distance table twice using a LEFT JOIN and then use the COALESCE() function to return the correct value for the distance :您可以使用LEFT JOIN两次加入location_distance表,然后使用COALESCE()函数返回distance的正确值:

select ot.id,
  ot.fromlocid,
  ot.tolocid,
  ot.otherdata,
  coalesce(ld1.distance, ld2.distance) distance
from other_table ot
left join location_distance ld1
  on ld1.fromLocid = ot.toLocid
  and ld1.toLocid = ot.fromLocid 
left join location_distance ld2
  on ld2.toLocid = ot.toLocid
  and ld2.fromLocid = ot.fromLocid 

See SQL Fiddle with Demo参见SQL Fiddle with Demo

This returns the result:这将返回结果:

| 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 |

It will probably be faster to join the distance table twice like两次加入距离表可能会更快

INNER JOIN location_distance ld1 ON ld1.fromLocid = ot.fromLocid AND ld1.toLocid = ot.toLocid
INNER JOIN location_distance ld2 ON ld2.toLocid = ot.fromLocid AND ld2.fromLocid = ot.toLocid

and then use an IF to determine which one to select然后使用 IF 来确定要选择哪个

IF(ld1.fromLocid, ld1.distance, ld2.distance) as distance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM