简体   繁体   English

PHP-比较两个MySQL表

[英]PHP - Compare two MySQL tables

Is there a PHP code that let's you compare 2 MySQL tables with each other and lets you add missing entries into one? 是否有PHP代码可让您将2个MySQL表彼此进行比较,并将缺失的条目添加到其中?

I have two tables. 我有两张桌子。 hs_hr_employee and rights. hs_hr_employee和权利。 I want to add data from certain columns from the hs_hr_employee table so that they would be the same in the rights tables. 我想从hs_hr_employee表中的某些列中添加数据,以便它们在权限表中是相同的。

hs_hr_employee has multiple rows, whereas the rights table has 5 rows. hs_hr_employee有多行,而权限表有5行。 The rights table gets the info from 4 columns from the hs_hr_employee table, emp_number, employee_id, emp_firstname, emp_lastname 权限表从hs_hr_employee表的emp_number,employee_id,emp_firstname,emp_lastname的4列中获取信息

Below is the code: 下面是代码:

    <?php

$connection = mysql_connect('localhost','admin','root');

if( isset($_POST['submit']) )
{
    if( isset( $_POST['cb_change'] ) && is_array( $_POST['cb_change'] ))
    {
        foreach( $_POST['cb_change']  as $emp_number => $permission)
        {
            $sql = "UPDATE `rights` SET Permission='".mysql_real_escape_string($permission)."' WHERE emp_number='".mysql_real_escape_string($emp_number)."'";
            echo __LINE__.": sql: {$sql}\n";
            mysql_query( $sql );
        }
    }
}
?>
<p style="text-align: center;">
    <span style="font-size:36px;"><strong><span style="font-family: trebuchet ms,helvetica,sans-serif;"><span style="color: rgb(0, 128, 128);">File Database - Administration Panel</span></span></strong></span></p>
<p style="text-align: center;">
    &nbsp;</p>

<head>
<style type="text/css">
table, td, th
{
border:1px solid #666;
font-style:Calibri;
}
th
{
background-color:#666;
color:white;
font-style:Calibri;
}
</style>
</head>

    <form method="post" action="admin.php">

    <?php 


        if (!$connection)
          {
          die('Could not connect: ' . mysql_error());
          }

        mysql_select_db('users', $connection);

        //mysql_query('INSERT into rights(Emp_num, ID, Name, Surname) SELECT emp_number, employee_id, emp_firstname, emp_lastname FROM hs_hr_employee');


        $result = mysql_query("SELECT emp_number, employee_id, emp_firstname, emp_lastname, Permissions FROM rights");

        mysql_query("INSERT INTO rights (emp_number, employee_id, emp_firstname, emp_lastname)
                    SELECT emp_number, employee_id, emp_firstname, emp_lastname
                    FROM hs_hr_employee
                    ON DUPLICATE KEY UPDATE employee_id = VALUES(employee_id), emp_number = VALUES(emp_number)
                    ");

        echo "<center>";

        echo "<table >
        <tr>
        <th>Employee Number</th>
        <th>ID</th>
        <th>Name</th>
        <th>Surname</th>
        <th>Permissions</th>
        <th>Change</th>
        </tr>";

        while($row = mysql_fetch_array($result))
          {
          echo "<tr>";
          echo "<td>" . $row['emp_number'] . "</td>";
          echo "<td>" . $row['employee_id'] . "</td>";
          echo "<td>" . $row['emp_firstname'] . "</td>";
          echo "<td>" . $row['emp_lastname'] . "</td>";
          echo "<td>" . $row['Permissions'] . "</td>";
          echo "<td> <select name='cb_change[]'><option value='all'>All</option> <option value='remote'>Remote Gaming</option> <option value='landbased'>Landbased Gaming</option> <option value='general'>General Gaming</option> </select> </td>"; 
          echo "</tr>" ;
          }


          #echo "<td>" . $row['Change'] . "</td>";


          echo "</table>";

          echo "</center>";


        #$_POST['cb_permissions'];


     mysql_close($connection);


    ?>

<p style="text-align: center;">
    &nbsp;</p>
<p style="text-align: center;">
    &nbsp;</p>

<p style="text-align: right;">
    <input name="Save_Btn" type="button" value="Save" />


    </p>


</form>

Any idea how to do it? 知道怎么做吗?

Thanks, Brian 谢谢,布莱恩

I'd go with an INSERT INTO SELECT here. 我将在这里使用INSERT INTO SELECT

Assume you recognize an employee by the Social Security Number (ssn), and you use this value to update, for instance, name and birthyear: 假设您通过社会安全号(ssn)识别了一名员工,并且使用此值来更新例如姓名和出生年月:

mysql_query("
INSERT INTO hs_hr_employee (ssn, name, birthyear)
SELECT ssn, name, birthyear
FROM hs_hr_rights
ON DUPLICATE KEY UPDATE name = VALUES(name), birthyear = VALUES(birthyear)
");

You can also add a WHERE in between FROM and ON DUPLICATE . 您还可以在FROMON DUPLICATE之间添加WHERE Like so: 像这样:

...
FROM hs_hr_rights
WHERE birthyear IS NULL
ON DUPLICATE KEY UPDATE name = VALUES(name), birthyear = VALUES(birthyear)
...

Although, I don't see the need of copying values since in most cases you can fetch them through JOIN s. 虽然,我看不到需要复制值,因为在大多数情况下,您可以通过JOIN获取它们。

select * from table a where a.common not in (select b.common from table b where a.common = b.common)

And run this as well 并同时运行

select * from table b where b.common not in (select a.common from table a where a.common = b.common)

Not possible in php but in mysql its ans is as above 在PHP中不可能,但是在MySQL中它的ans如上所述

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

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