简体   繁体   English

使用LIKE将数据从一个表匹配到另一个表

[英]matching data from one table to another with LIKE

I'm wondering you can match data from one table to another (example below) 我想知道您可以将一个表中的数据匹配到另一个表(以下示例)

What if I wanted to link my second table to the first one and add the id of the city to my headline. 如果我想将第二张表链接到第一张表并将城市ID添加到标题中,该怎么办。

I guess I would need to something like: 我想我需要这样的东西:

 SELECT * FROM headlines where headline LIKE %$city%

And then do an insert everything time I got something returned. 然后,每当我收到返回的内容时,都进行插入。 But then I would need to repeat my query for the hundred of cities I have every time I add a new headline. 但是,当我每次添加新标题时,都需要对数百个城市重复查询。

    First Table (Cities) | Second Table (Headlines)
    ______________________________________________________
    id: 1 name: New-York | Something happened in New-York
    id: 2 name: LA       | Something else happened in LA
                         | Somehting happened in LA and New-York

Can anybody point me to whatever I should be looking into to do this ? 有人可以指出我要做的一切吗?

You would use a join clause with a like condition: 您可以使用条件likejoin子句:

 SELECT h.*, c.cities
 FROM headlines h join
      cities c
      on h.headline like concat('%', c.city, '%')

The key is getting the wildcard parameters in the pattern, using concat() . 关键是使用concat()获得模式中的通配符参数。

By the way, this will do a nest-loop query, which will not perform very well. 顺便说一句,这将执行嵌套循环查询,但执行效果不佳。 Because of the like condition, indexes will not help improve performance. 由于情况like ,索引将无助于提高性能。

A better approach is to build a full text index on the headline column and use match for the comparison. 更好的方法是在headline列上建立全文索引,并使用match进行比较。 This will also help prevent conflicts like "London" matching "Londonderry". 这也将有助于防止诸如“伦敦”与“ Londonderry”匹配的冲突。

And, in your example LA is particularly problematic, because many words contain LA . 而且,在您的示例中LA尤其有问题,因为许多单词都包含LA You could try something like: 您可以尝试类似:

      on concat(' ', replace(replace(replace(h.headline, ',', ' '), ':', ' '), '.', ' '), ' ') like
               concat('% ', c.city, ' %')

That is, replace punctuation with spaces, add a space at the beginning and end of the headline, and then look for the city name surrounded by spaces. 也就是说,将标点符号替换为空格,在标题的开头和结尾处添加空格,然后查找由空格包围的城市名称。 This prevents a match between "LA" and "AtLAnta" (caps only for emphasis). 这样可以防止“ LA”和“ AtLAnta”之间的匹配(仅用于强调)。

you can try this 你可以试试这个

$sql = "SELECT * FROM cities";
$res_mysql=mysql_query($sql);  
   while($arr=mysql_fetch_array($res_mysql)){
      while(mysql_fetch_array($res_mysql_like)){
          $update= "UPDATE `Headlines` SET `id` ='".$arr[1]."'
          WHERE `headline LIKE %$arr[1]%";
      mysql_query($update)
      or die(mysql_error());
      }
   }

you need to select fisrt id fron city table and check if appear in headline table, then do the same white 2 id of table cities. 您需要选择城市ID的第一部分,并检查是否出现在标题表中,然后对表城市执行相同的白色2 ID。

i hope this can help you, maybe i didn't understand very well the problem, mi english level isn't very good. 我希望这可以为您提供帮助,也许我对这个问题不太了解,英语水平不是很好。

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

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