从MySQL中的多案例查询中删除重复项

时间:2015-04-07 08:24:09

标签: php mysql mysqli duplicates

我有以下查询

 SELECT
    student.StudentID,
    student.`Name`,
    CASE
WHEN attendance.date = '2015-09-07' and attendance.StudentID IS NOT NULL THEN
    'Present'
ELSE
    'Absent'
END AS '2015-09-07',
 CASE
WHEN attendance.date = '2015-09-14' and attendance.StudentID IS NOT NULL THEN
    'Present'
ELSE
    'Absent'
END AS '2015-09-14'
FROM
    student
LEFT JOIN attendance ON student.StudentID = attendance.StudentID`

这给了我以下结果:

Query Results

我尝试过使用GROUP BY student.StudentID但是它给了我不正确的结果。它在'2015-09-14'栏中显示'缺席'为'k1052280'而非现在。

我正在

enter image description here

我希望得到这个结果

enter image description here

  

CREATE TABLE学生( StudentID varchar(8) NOT NULL, 名称varchar(100) NOT NULL, 电子邮件varchar(254) CHARACTER SET latin1 NOT NULL, WorkshopID int(4) NOT NULL, PRIMARY KEY ( StudentID ), UNIQUE KEY StudentID ( StudentID ,电子邮件), KEY WorkshopID ( WorkshopID ), CONSTRAINT student_ibfk_1 FOREIGN KEY ( WorkshopID ) REFERENCES车间( WorkshopID ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

     

CREATE TABLE出勤( AttendanceID int(10) NOT NULL AUTO_INCREMENT, StudentID varchar(8) NOT NULL, 日期date NOT NULL, 时间time NOT NULL, PRIMARY KEY ( AttendanceID ), UNIQUE KEY unique_index ( StudentID , {日期{1}} {attendance_ibfk_1 {1}} {StudentID {1}} {学生{1}} {StudentID {1}}

     

), CONSTRAINT学生FOREIGN KEY (学生) REFERENCES学生(学生) ) ENGINE=InnoDB AUTO_INCREMENT=194 DEFAULT CHARSET=utf8;学生INSERT INTO学生VALUES ('k1052280', 'Ali Shaikh', 'k1052280@something.com', '101'); INSERT INTO学生VALUES ('k1052287', 'McKenzie Roth', 'Quisque@penatibus.edu', '102'); INSERT INTO学生VALUES ('k1052288', 'Dacey Sullivan', 'sollicitudin.adipiscing.ligula@semmollisdui.com', '103'); INSERT INTO {学生{1}}

     

VALUES ('k1052289', 'Callie Williamson', 'elementum@orciPhasellus.com', '104'); INSERT INTO出勤VALUES ('k1052290', 'Savannah Hyde', 'nec.metus.facilisis@nonummyut.co.uk', '101'); INSERT INTO出勤VALUES ('k1052291', 'Paul Tyson', 'semper.erat.in@ipsumleoelementum.net', '102'); INSERT INTO出勤VALUES ('k1052292', 'Nerea Ramos', 'gravida.sagittis.Duis@lacinia.edu', '103'); INSERT INTO出勤VALUES ('k1052293', 'Mark Mills', 'pellentesque.massa@blanditviverra.co.uk', '104'); INSERT INTO出勤VALUES ('k1052294', 'Zelda Cantu', 'ut@fringillaporttitorvulputate.org', '101');出勤INSERT INTO出勤VALUES ('168', 'k1052280', '2015-09-07', '00:00:00'); INSERT INTO出勤VALUES ('169', 'k1052287', '2015-09-09', '00:00:00'); INSERT INTO出勤VALUES ('170', 'k1052288', '2015-09-11', '00:00:00'); INSERT INTO出勤VALUES ('171', 'k1052289', '2015-09-11', '00:00:00'); INSERT INTO出勤VALUES ('172', 'k1052290', '2015-09-14', '00:00:00'); INSERT INTO出勤VALUES ('173', 'k1052291', '2015-09-16', '00:00:00'); INSERT INTO出勤VALUES ('174', 'k1052292', '2015-09-18', '00:00:00'); INSERT INTO出勤VALUES ('175', 'k1052293', '2015-09-18', '00:00:00'); INSERT INTO出勤VALUES ('176', 'k1052294', '2015-09-21', '00:00:00'); INSERT INTO出勤{ {1}}

1 个答案:

答案 0 :(得分:0)

这个查询可行 -

 SELECT Student.StudentID, Student.NAME, IF ( ( SELECT distinct 1 FROM attendance WHERE attendance.StudentID = student.StudentID AND date = '2015-09-07' ) = 1, 'Present', 'Absent' ) AS '2015-09-07', IF ( ( SELECT distinct 1 FROM attendance WHERE attendance.StudentID = student.StudentID AND date = '2015-09-14' ) = 1, 'Present', 'Absent' ) AS '2015-09-14' FROM student as student;

例 - 您可以在这样的列中使用子查询。

创建表:CREATE TABLE student(   id int(11)NOT NULL AUTO_INCREMENT,   name varchar(20)DEFAULT NULL,   主要关键(id) )ENGINE = InnoDB AUTO_INCREMENT = 8 DEFAULT CHARSET = latin1;

创建表:CREATE TABLE attend(   id int(11)NOT NULL AUTO_INCREMENT,   stud_id int(11)DEFAULT NULL,   date varchar(20)DEFAULT NULL,   PRIMARY KEY(id),   KEY stud_idstud_id),   约束attend_ibfk_1外键(stud_id)参考studentid) )ENGINE = InnoDB AUTO_INCREMENT = 11 DEFAULT CHARSET = latin1

插入数据 -

INSERT INTO studentidname)VALUES(1,' hitesh'),(2,' mundra'), (3,' sumit'),(4,' ashish');  INSERT INTO attendidstud_iddate)价值观(1,1,' 05-04-2015'),(2,1) ,' 05-04-2015'),(3,1,' 06-04-2015'),(4,1,' 06-04-2015' ;),(5,2,' 05-04-2015'),(6,3,' 05-04-2015'),(7,3,' 05-04-2015'),(8,4,' 06-04-2015');

结果查询

 select id , name , if((select distinct 1 from attend where stud_id=s.id and date = '05-04-2015')=1,'Present','Absent') as '05-04-2015' ,if((select distinct 1 from attend where stud_id=s.id and date = '06-04-2015')=1, 'Present' , 'Absent') as '06-04-2015' from student as s;