数据与另一个表进行比较

数据与另一个表进行比较

本文介绍了将 MySQL 数据与另一个表进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码从 tableOne 中回显了 8 个名称,但我想将这些名称与另一个表中的 8 个名称进行比较.我想将 $row['weight'] 中回显的行与 tableTwo 进行比较,如果结果不匹配,则添加 <spanclass="罢工"></span>$row['weight'] 中回显的结果.

This code below echoes out 8 names from tableOne, but I want to compare those names with 8 names in another table. I want to compare the rows echoed in $row['weight'] with tableTwo, and if the results don't match, then add a <span class="strike"> </span> to the result echoed in $row['weight'].

如何将 if/else 添加到 $row['weight'] 将每个名称与另一个表中的名称进行比较?

How do I go about adding an if/else to $row['weight'] compare each name with the names in another table?

$result = mysqli_query($con,"SELECT * FROM tableOne LIMIT 0, 8");

$i = 1;

while($row = mysqli_fetch_array($result)) {

  echo $i. " - " . $row['weight'] . '<br>';
  $i++;
}

推荐答案

这里有一些简单的代码可以帮助您入门:

Here is some simple code to get you started:

$result = mysqli_query($con,"SELECT * FROM tableOne LIMIT 0, 8");
$result2 = mysqli_query($con,"SELECT * FROM tableTwo LIMIT 0, 8");

$i = 1;

while($row = mysqli_fetch_array($result)) {
    $value1 = $row['weight'];
    $row2 = mysqli_fetch_array($result2);
    $value2 = $row2['weight'];
    echo $i . " - table 1: ";
    echo $value1;
    echo ", table 2: - ";
    if ($value2 != $value1) {
        echo '<span class="strike">$value2</span>';
    } else {
        echo $value2;
    }
    echo '<br>';
    $i++;
}

您可以使代码更智能,以处理没有 8 个要比较的值的情况,也可以在 HTML 表中显示这些值,但希望这可以帮助您入门.

You can make the code smarter, to handle cases where there aren't 8 values to compare, and to display the values in an HTML table too, but hopefully this can get you started.

这篇关于将 MySQL 数据与另一个表进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:04