如何使用NOT IN在MySQL的出勤表中错过几天

时间:2019-07-08 15:31:19

标签: mysql

我有一个每天参加的数据库。 因为从来没有学生,所以每天都是在日期时间(有人参加)字段中说明的。 因此,对于特定的学生证(sid),我认为我可以使用NOT IN来弥补出勤率的差距。

#include "pch.h"
#include "easendmailobj.tlh"
#include <tchar.h>
#include <iostream>
using namespace std;
using namespace EASendMailObjLib;
int _tmain(int argc, _TCHAR* argv[])
{

    ::CoInitialize(NULL);
    IMailPtr oSmtp = NULL;
    oSmtp.CreateInstance("EASendMailObj.Mail");//<- one cause of the error
    oSmtp->LicenseCode = _T("TryIt");
    //...
    _tprintf(_T("Start to send email ...\r\n"));//<- one cause of the error

但是,这产生的结果与我根本没有使用过NOT IN的结果相同。现在,我意识到无法打印那些缺少的日期,因为它们不在 sid 受限查询中。 我可以将NOT IN移到查询的前面,以便显示要显示的实际日期吗? 例如,该学生缺席2019-06-06(因为“他的”查询中缺少该日期,而其他学生缺席(因此嵌套查询 does 显示2019-06-06)

2 个答案:

答案 0 :(得分:0)

查看这个小提琴:https://www.db-fiddle.com/f/x8VVvMBmizmsbJRHmY1GEd/0

select date(absences) absences from (select distinct date(arrived) absences from attendance) t2
where date(t2.absences) not in (select date(arrived) from attendance where sid=38)  

答案 1 :(得分:0)

您需要交换内部和外部查询。您应该查找所有不在特定学生出勤天数内的学生的出勤天数(sid = 38):

select DISTINCT date(arrived) as 'day' 
from attendance 
where date(arrived) NOT IN (
    select date(arrived) 
    from attendance 
    where sid = '38'
      and MONTH(arrived) = 6
  ) 
  and MONTH(arrived) = 6