简体   繁体   English

MySQL用另一张表替换列中的逗号分隔值,找到旧值替换为新值

[英]MySQL replace comma separated values in column with another table find old value to replace with new

MySQL table want to replace comma separated values in column with another table value MySQL表希望用另一个表值替换列中的逗号分隔值

I have two table 我有两个桌子

  • 1st table column which has comma separated values(hotkey). 具有逗号分隔值(热键)的第一表列。
  • 2nd Table have oldid & Newid column 第二个表具有oldid和Newid列

I want to search 1st table column with replace oldid to newid 我想搜索第一表列,其中将oldid替换为newid

action table(1st) 行动表(第一)

id    hotkey
===   ======
1    2,3,4,5
2    3,2,14,7
3    4,5,6,11
4    9,2,11,5
5    11,5,3,8

tempID table(2nd) tempID表(第二个)

id  oldid   newid 
===  ===    === 
1    5      4  
2    7      6  
3    3      8  
4    9      12  
5    11     14  

OUTPUT table (Desired) OUTPUT表(所需)

id    hotkey
===   ======
1    2,8,4,4
2    8,2,14,6
3    4,4,6,14
4    12,2,14,4
5    14,4,8,8

I Solve by using PHP Script as follows: 我通过使用PHP脚本解决如下:

//db access
    $dbhost = "localhost";
    $dbuser   = "root";
    $dbpassword = "";
    $database = "dbaname";
    $dbcon = mysql_connect($dbhost, $dbuser, $dbpassword) or die(mysql_error());
    mysql_select_db($database,$dbcon) or die ('>>>'.mysql_errno()."Error1 :".mysql_error());

    //find & Replace 
    $q=mysql_query("SELECT oid,nid FROM tempid") or die(mysql_error());
    while($fetch_com=mysql_fetch_array($q))   {  
        $oid=$fetch_com['oid'];
        $nid=$fetch_com['nid'];     
        mysql_query("UPDATE action SET hotkey=REPLACE(hotkey,',$oid,',',$nid,')");  
    }

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

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