dtkvlj5386 2014-05-21 01:14
浏览 82
已采纳

如何比较PHP中两个表中的两个字段?

Table_1

ID | Username
 1   John
 2   Mike
 3   Chase
 4   Shane

Table_2

 ID | Username
 1  | John
 2  | Kenny
 3  | Chase
 4  | Shane

I want to get ID from Table_1. Then find that ID in table_2. Then i need it to look at the field Username in both tables. and if they match do nothing, If they don't match then update it to the username in Table_2 and run some code (like email me)

I need to to check every row in Table_1 everytime I run the script.

  • 写回答

2条回答 默认 最新

  • drl47263 2014-05-21 01:26
    关注

    You have to use INNER JOIN in case to find the matching ID in both the tables and if you got one then update.

    $sql = mysql_query("SELECT table_1.id as id_one,table_1.name as name_one,table_2.name as name_two FROM table_1 INNER JOIN table_2 ON (table_1.id = table_2.id)") or die(mysql_error());
    
        if(mysql_num_rows($sql) > 0)
        {
             while($fetch = mysql_fetch_assoc($sql))
             {
                   if($fetch['name_one'] != $fetch['name_two'])
                   {
                         // UPDATE table_1 'name' FIELD
    
                           mysql_query("UPDATE table_1 SET name = '".$fetch['name_two']."' WHERE id = ".$fetch['id_one']) or die(mysql_error());
    
                              // DO WHATEVER YOU WANT
                   }
    
             }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向