1

I have two for each loop . Each one is running on a different table

$nores_std="SELECT std_id,std_name FROM student WHERE std_class = '$cl' ";
$nores_q = mysqli_query($link,$nores_std);
foreach ($nores_q as $name_std){
echo $name_std['std_id']; echo '<br>';
}
echo '<br>';
$no_select = "SELECT std_id,std_name FROM selection WHERE std_class = '$cl' ";
$no_select_q = mysqli_query($link,$no_select);
foreach ( $no_select_q as $std_sel){
    echo $std_sel['std_id'];
    echo '<br>';
}

The firs result is:

**554553**
**554557**
554561
**554566**
554573
**554577**
554581
554582
554587
**554590**
554591
554593
554596
554597
554599
554602
554603
554604
554607
554619
554620
554632
554637
554640
554643
554648
**554549**

And the second result is:

554566
554577
554549
554590
554553
554557

I would like to check for duplication and remove the duplication values, display only the one that are not duplicated in

4

2 回答 2

1

PHP解决方案是array -diff

$nores_std="SELECT std_id,std_name FROM student WHERE std_class = '$cl' ";
$nores_q = mysqli_query($link,$nores_std);
foreach ($nores_q as $name_std){
    $a[] = $name_std['std_id'];
}

$no_select = "SELECT std_id,std_name FROM selection WHERE std_class = '$cl' ";
$no_select_q = mysqli_query($link,$no_select);
foreach ( $no_select_q as $std_sel){
    $b[] = $std_sel['std_id'];
}

$res = array_diff($a, $b);

注意您可以使用数组列来避免foreach循环

我会重新检查数据方案- 您可能希望以不同的方式获取数据

于 2019-02-18T14:55:02.633 回答
0

最有效的方法是在单个查询中从两个表中选择不同的:

SELECT DISINCT std_id, std_name FROM
(
        SELECT std_id, std_name FROM student   WHERE std_class = '$cl'
  UNION SELECT std_id, std_name FROM selection WHERE std_class = '$cl'
) t
于 2019-02-18T15:02:33.870 回答