MYSQL匹配2个表之间的记录

时间:2011-10-14 12:21:15

标签: mysql

我有2张桌子

tb1
OLD_id | East | North
1      | 1000 | 2000

tb2
NEW_id | East | North
4000   | 1009 | 1995

对于“tb1”中的所有记录,我需要找到符合以下条件的“tb2”中的所有记录

tb2.East between (tb1.East - 10) and (tb1.East + 10)
tb2.North between (tb1.North - 10) and (tb1.North + 10)

输出

OLD_id | Matching NEW_ID's | East Difference | North Difference
1      | 4000              | 9               | -5

3 个答案:

答案 0 :(得分:1)

试试这个 -

SELECT t1.OLD_id, t2.new_id `Matching NEW_ID''s`, t2.East - t1.East `East Difference`, t2.North - t1.North `North Difference` FROM tb1 t1
  JOIN tb2 t2
  ON 
    t2.East BETWEEN t1.East - 10 AND t1.East + 10 AND
    t2.North BETWEEN t1.North - 10 AND t1.North + 10

答案 1 :(得分:0)

尝试使用此查询

select * 
from 
tb1 
  join tb2 on (tb2.East between tb1.East - 10 and tb1.East + 10) 
  and (tb2.North between   tb1.North - 10 and tb1.North + 10)

答案 2 :(得分:0)

试试这个

SELECT tb1.OLD_id,tb2.NEW_id AS Matching_NEW_ID's,(tb1.East-tb2.East) AS East_Difference, (tb2.North-tb1.North) AS North_Difference FROM tb1,tb2 WHERE "CONDITIONS"