-3

I want to compare the two data and if the data are identical with each other, write the name of the data

How can a better query?

For Example:

 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";
}
0

1 Answer 1

1

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']; 
    }
}

With this query you will only get the data you actually want, so less data moved between database server and script, and less code to manipulate it

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.