2013-03-03 301 views
0

后搜索这是SQL查询,我可以使用第一使用SUBSTRING_INDEX第一个逗号和第二个逗号

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

作为一个例子上面的查询将显示曼彻斯特,因为它是第一位的列中:曼彻斯特,德比,伯明翰

但我也想一步一个查询,可以搜索德比或伯明翰所以如果需要的话可以超越第一个逗号。

+0

只是为了澄清我想能够查询以上在第一个逗号或第二个逗号后面说。 – 2013-03-03 22:51:46

回答

0

这里有一种方法:

update Table1 t1 
     inner join Table2 t2 
     on find_in_set(t2.location, t1.Location) > 0 
    SET t1.Latitude = t2.Latitude , 
     t1.Longitude = t2.Longitude 

这也可以让你搜索一个特定的位置,如2号:

update Table1 t1 
     inner join Table2 t2 
     on find_in_set(t2.location, t1.Location) = 2 
    SET t1.Latitude = t2.Latitude , 
     t1.Longitude = t2.Longitude 
+0

在大型数据集上,这是非常缓慢的性能表现。有没有使用全文索引的方法? – 2013-03-04 09:14:24

+0

@ user1967987。 。 。如果性能是一个问题,那么你应该考虑在t1中正确地规范位置列表。这可能比将全文搜索应用于标签列表更好。 – 2013-03-04 14:05:34