如何比较PHP中的两个表并显示表1中不匹配的软件名称?

时间:2015-05-06 06:57:54

标签: php mysql

This is Table 1

 This is Table1

This is Table 2

 This is Table2

我想将table2与table1进行比较,并在表2中显示表1中不存在的软件名称。

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

  This is Table2

 <?php

$link = mysqli_connect("localhost", "root", "", "audit");

 // Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$softwarename = mysqli_real_escape_string($link, $_POST['softwarename']);
// attempt insert query execution
$sql = "INSERT INTO licence (softwarename) VALUES ('$softwarename')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);

?

答案 2 :(得分:0)

 this is Table1

<?php

$link = mysql_connect("localhost", "root", "");
mysql_select_db("audit", $link);
//$name = $_POST['name'];
$sql = "SELECT * FROM report";
$res = mysql_query($sql,$link);
$softwares = array();
$count = 0;
while ($result = mysql_fetch_array($res)) {
    $softwares[] = $result;
}

echo "<table border=1>";

echo "<th>IP address</th>";
echo "<th>MAC address</th>";
echo "<th>Software</th>";
echo "<th>Version</th>";

foreach ($softwares as $sw) {
    $cols = count($sw);
   // echo $sw['ipaddress']."<br>";
    //echo $sw['macaddress']."<br>";

    //echo $cols;
    for ($i = 1; $i <= $cols; $i++) {

        if (!empty($sw['software'.$i])) {
            echo "<tr>";           
            echo "<td>".$sw['ipaddress']."</td>";
            echo "<td>".$sw['macaddress']."</td>";

            echo "<td>".$sw['software'.$i]."</td>";
            echo "<td>".$sw['version'.$i]."</td>";

            echo "</tr>";

            $count = $count+1;

        }

    }
}
echo $count;
echo "</table>";

?>

答案 3 :(得分:0)

使用sql查询实现目的的简单方法。将查询集成到您的php脚本中以获取报告。

master1

+----+------+
| id | name |
+----+------+
|  1 | A    |
|  2 | B    |
|  3 | C    |
|  4 | D    |
|  5 | E    |
|  6 | F    |
+----+------+

SLAVE1

+----+-------+-------+-------+
| id | name1 | name2 | name3 |
+----+-------+-------+-------+
|  1 | A     | C     | B     |
|  2 | B     | D     | G     |
|  3 | O     | A     | D     |
|  4 | E     | F     | M     |
+----+-------+-------+-------+

这是您的解决方案

SELECT slave1.id, slave1.name1 AS name FROM slave1 LEFT JOIN master1 AS m1 ON slave1.name1 = m1.name WHERE m1.name IS NULL
UNION
SELECT slave1.id, slave1.name2 AS name FROM slave1 LEFT JOIN master1 AS m2 ON slave1.name2 = m2.name WHERE m2.name IS NULL
UNION
SELECT slave1.id, slave1.name3 AS name FROM slave1 LEFT JOIN master1 AS m3 ON slave1.name3 = m3.name WHERE m3.name IS NULL

+----+------+
| id | name |
+----+------+
|  3 | O    |
|  2 | G    |
|  4 | M    |
+----+------+

由于