PHP和MySQL从一个表中选择另一个表中不存在的记录

时间:2017-04-30 14:11:49

标签: php mysql

我有两个表,第一个叫做new_account,另一个叫new_record

new_account包含注册用户的信息,表new_record包含已经付费的new_account表中的用户信息以及他们已付款的月份。< / p>

我希望能够根据输入中输入的月份和年份查看未付款的用户。这是我正在使用的PHP:

$query= 'SELECT * FROM   new_account LEFT OUTER JOIN new_record 
                      where month=$_POST[month] and year =$_POST[year] 
                      ON (new_account.mbere = new_record.mbere)
                      WHERE new_record.mbere IS NULL ';

2 个答案:

答案 0 :(得分:1)

对于我从您的查询中可以理解的内容,这至少应该修复您的语法。

$query= 'SELECT * 
FROM  new_account 
LEFT OUTER JOIN new_record ON new_account.mbere = new_record.mbere 
WHERE new_record.mbere IS NULL AND month='.$_POST['month'].' AND year = '.$_POST['year'].';

您的查询中不能有多个WHERE子句,因此我将它们分组为togheter。我还修改了单引号的变量转义

答案 1 :(得分:0)

陈述的结构应该是这样的:


    SELECT *
    FROM new_account
    LEFT JOIN new_record
    ON new_account.mbere=new_record.mbere
    WHERE new_record.mbere IS NULL
    AND new_recor.month ='$_POST[month]'
    AND new_recor.year = '$_POST[year]';


对此的查询应该用作:


    $month = $_POST[month];
    $year = $_POST[year];
    $query = "SELECT * FROM new_account LEFT JOIN new_record ON new_account.mbere=new_record.mbere WHERE new_record.mbere IS NULL AND new_record.month ='$month' AND new_record.year = '$year'";