0

我环顾四周,似乎找不到以下问题的答案:

我想要做的就是运行一个 SQL 选择语句,向用户显示提供发票编号(在本例中为唯一 ID)的内容未显示在同一数据库的另一个表中,目前代码如下:

$query = mysql_query("SELECT `ordate`, `invno` FROM `armast09` WHERE cshipno = '$username' AND `ordate` > DATE_SUB(CURDATE(), INTERVAL 4 DAY)") or die(header("location:index.php?e=4"));   
$numrows = mysql_num_rows($query);

$date = date('o-n-d');

include 'indexfiles/top.php';

    echo "The following deliveries are available for viewing / dispute:";
    echo "<br />";  
    echo "<br />";
    echo "<center>";
    echo "<table>"; 
        if($numrows != 0) {                 
            while ($info = mysql_fetch_array($query)) {
                echo "<tr>";                            
                    echo "<form action = \"deliverydispute.php?invno=".$info['invno']."\" method=\"POST\ onsubmit=\"return confirm('Are you sure you are ready to dispute? You can't go back after this.');\">";
                        echo "<td><b>Invoice Number: </b>".$info['invno']."</td>";
                        echo "<td><b>Order Date: </b>".$info['ordate']."</td>";                                 
                        echo "<td><a href = \"deliverydispute.php?invno=".$info['invno']."&&ordate=".$info['ordate']."\">Dispute</a></td>";
                    echo "</form>";                         
                echo "</tr>"; 
            }
        } else {
            echo ("<font color=\"red\" face=\"arial\" size=\"2\"><small>No orders are currently available for viewing.</small></font>");
        }
    echo "</table>";
4

2 回答 2

1

您可以使用NOT IN谓词:

SELECT ordate, invno
FROM armast09
WHERE cshipno = '$username' 
  AND ordate > DATE_SUB(CURDATE(), INTERVAL 4 DAY) 
  AND invno NOT IN(SELECT invno FROM Anothertable WHERE invno IS NOT NULL);

或者LEFT JOIN

SELECT a.ordate, a.invno 
FROM armast09 a
LEFT JOIN anothertable a2 ON a.invno = a2.invno
WHERE a.cshipno = '$username' 
  AND a.ordate > DATE_SUB(CURDATE(), INTERVAL 4 DAY) 
  AND a.invno IS NULL;
于 2013-01-20T00:37:08.217 回答
0

您可以使用以下方法轻松完成此操作:

SELECT X FROM TABLEA LEFT JOIN TABLEB ON TABLEA.ID = TABLEB.ID WHERE TABLEB.ID is null

应该这样做!

于 2013-01-20T00:37:31.820 回答