通过全文搜索加入两个表

时间:2013-03-03 16:59:29

标签: mysql full-text-search left-join inner-join

此数据库仅用于合并两个表,其中包含位置和其他纬度和经度值。我想要做的是按位置连接两个表,以便将纬度和经度值添加到表1。

表1:

Title:                Location:                          Latitude:       Longitude
pizza shop            london, chelsea, el13 4hr          Null            Null
Phone Shop            Manchester - Derby                 Null            Null
Computer Repair       Birmingham (b70)                   Null            Null


Table 2:

Location             Latitude:          Longitude:
London               53.6658            0.25533
birmingham           54.3665            0.89336
manchester           66.3368            0.25836

在某些情况下,表1的位置列可以包含使用逗号破折号或多于一个位置的数据,因此我选择使用全文来匹配首次匹配并显示经度和纬度值。

表1的最终结果应为:

Title:                Location:                          Latitude:       Longitude
pizza shop            London                             53.6658         0.25533
Phone Shop            manchester                         66.3368         0.25836
Computer Repair       birmingham                         54.3665         0.89336

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

你可能正在寻找这个

    select t1.Title , SUBSTRING_INDEX(t1.Location, ' ', 1)as Location , t2.Latitude  , t2.Longitude 
   from Table1 t1
  inner join
  Table2 t2
   on  SUBSTRING_INDEX(t1.Location, ' ', 1) = t2.Location

输出:

TITLE               LOCATION       LATITUDE     LONGITUDE
pizza shop          london         53.6658      0.25533
Computer Repair     Birmingham     54.3665      0.89336
Phone Shop          Manchester     66.3368      0.25836

DEMO SQLFIDDLE HERE

带有UPDATE声明的

应该是那样的

    update Table1 t1
        inner join Table2 t2
        on  SUBSTRING_INDEX(t1.Location, ' ', 1) = t2.Location 
    SET t1.Latitude = t2.Latitude ,
        t1.Longitude = t2.Longitude

DEMO SQLFIDDLE

答案 1 :(得分:0)

select t1.title, t2.location, t2.latitude, t2.longitude
from table1 t1, table2 t2
where t1.location ilike concat('%', t2.location, '%');
相关问题