【问题标题】:How to comapare two different colomns of two different table in sql using php?如何使用php比较sql中两个不同表的两个不同列?
【发布时间】:2018-02-23 03:55:42
【问题描述】:

我使用此代码从表“empdet”中获取数据并将它们插入到另一个表“检查更新”中。 我想比较这两个表的两个列,并从表'empdet'中回显不同的列

ss.php

  <?php
 require "init.php";
$sql = "SELECT * FROM empdet";
$result = $con->query($sql);

if ($result->num_rows > 0) {
    // output data of each row

    while($row = mysqli_fetch_assoc($result))
        {
            echo "id: " . $row["id"]. " - Employeeid: " . $row["Employeeid"]. " " . $row["last_update"]. "<br>";
                $sqls = "INSERT INTO Check_Update (Employeeid, Schduleid, Check_update)
                VALUES ('".$row[Employeeid]."', '". $row[id]."', '".$row[last_update]."')";
                echo $sqls.'<br>';      
                $res =$con->query($sqls);

    }

}
 else {
    echo "0 results";
}

$con->close();
   ?>

数据库表

例如。 我想将表 'empdet' 的列 'last_update' 与表 'check update' 的列 'check_update' 进行比较,以便找到这两个列之间的区别,并且可以将行放在表 'empdate 的回显中'。 就像,'empdat' 的最后两行发生了变化,即 'last_update' ,所以当我与列 'check_update' 进行比较时,它应该回显 'empdat' 的最后两行并且它应该更新最后两行'check_update' 列。 我的问题是 1)如何比较两个表的两个列? 2)如何让他们回声? 3)发现差异后如何更新'check_update'列?

我是 mysql 查询的初学者,谁能帮我做这个?

【问题讨论】:

标签: php mysql


【解决方案1】:
Try this:
require "init.php";
$sql = "select e.id ,e.Employeeid,e.last_update from empdel AS e inner join Check_Update.* AS c on c.Scheduleid = e.id
where e.last_update != c.Check_Update";
$result = $con->query($sql);

if ($result->num_rows > 0) 
{
    // output data of each row
    while($row = mysqli_fetch_assoc($result))
    {
            echo "id: " . $row["id"]. " - Employeeid: " . $row["Employeeid"]. " " . $row["last_update"]. "<br>";

            $sqls =  "update Check_Update set Check_Update = '".$row['last_update']."' where Scheduleid = '".$row["id"]."' "
            echo $sqls.'<br>';      
            $res =$con->query($sqls);
    }
}
else 
{ 
   echo "0 results";
}

$con->close();

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多