比较两个数据- MySQL &PHP


Compare the two data - MySQL & PHP

我想比较两个数据,如果数据完全相同,请写出数据的名称

如何才能更好的查询?

例如:

 ExampleTable1 * ID    = 5  --
                              | (5=5) ---> "Hello This Five" (vtname)
 ExampleTable2 * BEID  = 5  --
SQL:

$sql1 = "SELECT * FROM ExampleTable1 WHERE paid= ".$paidsi;
$result1 = mysqli_query($conn, $sql1); 
if (mysqli_num_rows($result1) > 0) { 
    while($row1 = mysqli_fetch_assoc($result1)) {
        $sql2 = "SELECT * FROM ExampleTable2";
        $result2 = $conn->query($sql2); 
            if ($result2->num_rows > 0) { 
                while($row2 = $result2->fetch_assoc()) {
                    if ($row2["id"] == $row1["beid"]) {
                        echo $row2["vtname"]; 
                    } else {
                    echo "";
                    } 
                }
            } else {
                echo "0 results";
            }   
    }
} else {
    echo "0 results";
}

use left join

$sql1 = "SELECT * FROM ExampleTable1 E1
            LEFT JOIN ExampleTable2 E2 ON E1.id = E2.beid";
$result1 = mysqli_query($conn, $sql1); 
if (mysqli_num_rows($result1) > 0) { 
    while($row1 = mysqli_fetch_assoc($result1)) {
        echo $row1['vtname']; 
    }
}

使用此查询,您将只获得实际需要的数据,因此在数据库服务器和脚本之间移动的数据更少,操作它的代码更少