PHP - 比较两个 MySQL 表 [英] PHP - Compare two MySQL tables

查看:45
本文介绍了PHP - 比较两个 MySQL 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 PHP 代码可以让您将 2 个 MySQL 表相互比较,并将缺失的条目添加到一个表中?

Is there a PHP code that let's you compare 2 MySQL tables with each other and lets you add missing entries into one?

我有两张桌子.hs_hr_employee 和权利.我想从 hs_hr_employee 表的某些列添加数据,以便它们在权限表中相同.

I have two tables. hs_hr_employee and rights. 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 有多行,而权限表有 5 行.权限表从 hs_hr_employee 表的 4 列中获取信息,emp_number、employee_id、emp_firstname、emp_lastname

hs_hr_employee has multiple rows, whereas the rights table has 5 rows. The rights table gets the info from 4 columns from the hs_hr_employee table, emp_number, employee_id, emp_firstname, emp_lastname

代码如下:

    <?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>

知道怎么做吗?

谢谢,布莱恩

推荐答案

我会在这里使用 INSERT INTO SELECT.

假设您通过社会安全号码 (ssn) 识别出一名员工,并且您使用此值来更新姓名和出生年份:

Assume you recognize an employee by the Social Security Number (ssn), and you use this value to update, for instance, name and birthyear:

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)
");

您还可以在FROMON DUPLICATE 之间添加WHERE.像这样:

You can also add a WHERE in between FROM and ON DUPLICATE. Like so:

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

虽然,我认为不需要复制值,因为在大多数情况下,您可以通过 JOIN 获取它们.

Although, I don't see the need of copying values since in most cases you can fetch them through JOINs.

这篇关于PHP - 比较两个 MySQL 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆