sql查询比较两个表的数据并返回那些不匹配的表

时间:2015-02-19 12:33:10

标签: php mysql

表1

收据1 - book1

700 - 100

701 - 100

702 - 100

表2

book2 receipt2

100 700,701

当我输入bookno = 100并点击搜索然后它匹配table1和table2并返回结果

注意:在table2中,receipt2列的数据采用逗号分解形式....如701,702,703

情景1

如果Table2不为空白反对输入的bookno,则仅返回以下结果

702

Scenario2

如果Table2为空白反对输入的bookno然后返回 700701702

场景3

如果Table2包含table1的所有值,则反对输入的bookno,然后返回

0

<?php
   if($_POST && isset($_POST['search']))
    {   

        $result=$db->prepare('SELECT DISTINCT master.receipt_no FROM scheme_master AS MASTER 
JOIN book_issue_return AS bir ON master.book_no2 = bir.book_no
WHERE master.book_no2 = :book_no2 AND (master.receipt_no NOT IN (SELECT bir.receiptno FROM book_issue_return AS bir))
');
        $result->bindParam(':book_no2',$_POST['book_no']);
        $result->execute(); 
        $data1 = $result->fetchAll();
        $coupons = array(); 
        foreach($data1 as $row)
        {    
            $receipt[] = $row['receipt_no'];            
        }   
    }
?>

1 个答案:

答案 0 :(得分:0)

试试这个会起作用:

  SELECT `t1`.* FROM Table1 t1
    LEFT JOIN Table2 `t2`
    ON `t1`.Book != `t2`.Book