Php 比较两个表并从一个表中选择数据

Php 比较两个表并从一个表中选择数据,php,mysqli,Php,Mysqli,我有两个表lc\u details和reference,它们有reference\u no列。如果lc\u详细信息,我想比较这些表并从reference表中选择reference\u no。reference\u no列中不存在reference\u no 这是我的代码,但它显示了参考表中的所有数据,并重复了更多次 <table class="table table-striped"> <tr> <th>

我有两个表
lc\u details
reference
,它们有
reference\u no
列。如果
lc\u详细信息,我想比较这些表并从
reference
表中选择
reference\u no
。reference\u no
列中不存在reference\u no

这是我的代码,但它显示了参考表中的所有数据,并重复了更多次

        <table class="table table-striped">
        <tr>
            <th>Sl. No.</th>
            <th>Reference No./th>

        </tr>
        <?php
        include("../db/db.php");

        $sql_cat="select b.* from lc_details a, reference b where b.reference_no!=a.reference_no' order by reference_no ASC";
        $run_sql=mysqli_query($con, $sql_cat);

        $i=0;

        while($row_cat=mysqli_fetch_array($run_sql)){
            $reference_no=$row_cat['reference_no'];

            $i++;


        ?>

        <tr align="center">
            <td><?php echo $i; ?></td>
            <td><?php echo $reference_no; ?></td>
           </tr>
           <?php } ?>
        </table>

序号。
参考编号/th>
试试这个:

SELECT ref.* 
FROM reference AS ref
LEFT JOIN lc_details AS lc_d
ON lc_d.reference_no = ref.reference_no
WHERE lc_d.reference IS NULL
ORDER BY ref.reference_no ASC
让我解释一下:

使用left join连接一个表意味着,如果左表(在我们的示例中为'reference'表)中不存在右表(在我们的示例中为'lc_details表)值或引用,它仍将获取记录。例如:

如果表A包含ID为1、2、3和4,而表B仅包含ID为1、2和4,则使用left join连接它们将产生如下结果:

A.id   B.id
1      1
2      2
3      NULL
4      4
因此,添加一个where子句,只检查表b上id为null的那些将导致:

A.id   B.id
3      NULL
了解更多关于