我试图比较tableOne
中的名称与tableTwo
中的名称。下面的代码做到了这一点,但它只比较相应行中的名称。我想扫描tableTwo
,看看它们是否与tableOne
中的名称匹配,如果匹配,则向tableOne
中的名称添加一个strike。
$result = mysqli_query($con,"SELECT * FROM tableOne LIMIT 0, 8");
$result2 = mysqli_query($con,"SELECT * FROM tableTwo");
$i = 1;
while($row = mysqli_fetch_array($result)) {
$value1 = $row['names'];
$row2 = mysqli_fetch_array($result2);
$value2 = $row2['names'];
echo $i . " - ";
if ($value2 != $value1) {
echo '<span class="strike">' . $value1 . '</span>';
} else {
echo $value2;
$points = 7;
echo '<span class="champ">' . '+' . $points . "</span>";
}
echo '<br>';
$i++;
}
我在这里问了一个类似的问题,但没有完全做到我需要做的。我相信我需要做一个嵌套循环,但我不确定如何实现它。
最好是编写一个更复杂的查询,并且只循环一次。
下面的查询将'names'列匹配的tableOne和tableTwo行连接在一起。如果它们不匹配,那么tableOne列将存在,而tableTwo列将全部为空。
$sQuery = "SELECT tableOne.*, tableTwo.names as t2names
FROM tableOne LEFT JOIN tableTwo USING (names) LIMIT 0, 8";
$result = mysqli_query($con,$sQuery);
$i = 1;
while($row = mysqli_fetch_array($result)) {
$value1 = $row['names'];
echo $i . " - ";
if ($row['t2names']) {
echo '<span class="strike">' . $value1 . '</span>';
} else {
echo $value1;
$points = 7;
echo '<span class="champ">' . '+' . $points . "</span>";
}
echo '<br>';
$i++;
}
我很少使用mysql,所以查询可能不能完全工作
再次阅读您的问题,我认为array_intersect
是您更好的选择。
$result = mysqli_query($con, "SELECT * FROM tableOne LIMIT 0, 8");
$result2 = mysqli_query($con, "SELECT * FROM tableTwo");
while ($row = mysqli_fetch_array($result)) {
$array1[] = $row['names'];
}
mysqli_free_result($result);
while ($row = mysqli_fetch_array($result2)) {
$array2[] = $row['names'];
}
mysqli_free_result($result2);
$intersect = array_intersect($array1, $array2);
当我自己使用PDO时,你基本上得到独立的数组,然后将它们相交。
php: array_intersect
当你把你的数组整理好后,你可以添加你的罢工。