从 MySQL 中的多案例查询中删除重复项
Remove Duplicates from multi case query in MySQL
我有以下查询
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`
这给了我以下结果:
我试过使用 GROUP BY student.StudentID
,但结果不正确。它在 'k1052280' 的“2015-09-14”列中显示 'Absent' 而不是现在。
我得到
我想得到这个结果
CREATE TABLE
student(
StudentIDvarchar(8) NOT NULL,
Namevarchar(100) NOT NULL,
Emailvarchar(254) CHARACTER SET latin1 NOT NULL,
WorkshopIDint(4) NOT NULL,
PRIMARY KEY (
StudentID),
UNIQUE KEY
StudentID(
StudentID,
Email),
KEY
WorkshopID(
WorkshopID),
CONSTRAINT
student_ibfk_1FOREIGN KEY (
WorkshopID) REFERENCES
workshop(
WorkshopID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE
attendance(
AttendanceIDint(10) NOT NULL AUTO_INCREMENT,
StudentIDvarchar(8) NOT NULL,
Datedate NOT NULL,
Timetime NOT NULL,
PRIMARY KEY (
AttendanceID),
UNIQUE KEY
unique_index(
StudentID,
Date),
CONSTRAINT
attendance_ibfk_1FOREIGN KEY (
StudentID) REFERENCES
student(
StudentID)
) ENGINE=InnoDB AUTO_INCREMENT=194 DEFAULT CHARSET=utf8;
INSERT INTO
studentVALUES ('k1052280', 'Ali Shaikh', 'k1052280@something.com', '101');
INSERT INTO
studentVALUES ('k1052287', 'McKenzie Roth', 'Quisque@penatibus.edu', '102');
INSERT INTO
studentVALUES ('k1052288', 'Dacey Sullivan', 'sollicitudin.adipiscing.ligula@semmollisdui.com', '103');
INSERT INTO
studentVALUES ('k1052289', 'Callie Williamson', 'elementum@orciPhasellus.com', '104');
INSERT INTO
studentVALUES ('k1052290', 'Savannah Hyde', 'nec.metus.facilisis@nonummyut.co.uk', '101');
INSERT INTO
studentVALUES ('k1052291', 'Paul Tyson', 'semper.erat.in@ipsumleoelementum.net', '102');
INSERT INTO
studentVALUES ('k1052292', 'Nerea Ramos', 'gravida.sagittis.Duis@lacinia.edu', '103');
INSERT INTO
studentVALUES ('k1052293', 'Mark Mills', 'pellentesque.massa@blanditviverra.co.uk', '104');
INSERT INTO
studentVALUES ('k1052294', 'Zelda Cantu', 'ut@fringillaporttitorvulputate.org', '101');
INSERT INTO
attendanceVALUES ('168', 'k1052280', '2015-09-07', '00:00:00');
INSERT INTO
attendanceVALUES ('169', 'k1052287', '2015-09-09', '00:00:00');
INSERT INTO
attendanceVALUES ('170', 'k1052288', '2015-09-11', '00:00:00');
INSERT INTO
attendanceVALUES ('171', 'k1052289', '2015-09-11', '00:00:00');
INSERT INTO
attendanceVALUES ('172', 'k1052290', '2015-09-14', '00:00:00');
INSERT INTO
attendanceVALUES ('173', 'k1052291', '2015-09-16', '00:00:00');
INSERT INTO
attendanceVALUES ('174', 'k1052292', '2015-09-18', '00:00:00');
INSERT INTO
attendanceVALUES ('175', 'k1052293', '2015-09-18', '00:00:00');
INSERT INTO
attendanceVALUES ('176', 'k1052294', '2015-09-21', '00:00:00');
INSERT INTO
attendanceVALUES ('177', 'k1052295', '2015-09-23', '00:00:00');
INSERT INTO
attendanceVALUES ('178', 'k1052296', '2015-09-25', '00:00:00');
INSERT INTO
attendanceVALUES ('179', 'k1052297', '2015-09-25', '00:00:00');
INSERT INTO
attendanceVALUES ('183', 'k1052288', '2015-09-14', '00:00:00');
INSERT INTO
attendanceVALUES ('187', 'k1052290', '2015-09-07', '00:00:00');
INSERT INTO
attendanceVALUES ('188', 'k1052280', '2015-09-21', '10:30:00');
INSERT INTO
attendanceVALUES ('193', 'k1052280', '2015-04-05', '00:00:00');
这个查询可以工作-
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;
例子-
您可以像这样在列中使用子查询。
创建 Table: 创建 TABLE student
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(20) 默认为空,
主键 (id
)
) ENGINE=InnoDB AUTO_INCREMENT=8 默认字符集=latin1;
创建 Table: 创建 TABLE attend
(
id
int(11) NOT NULL AUTO_INCREMENT,
stud_id
int(11) 默认为空,
date
varchar(20) 默认为空,
主键(id
),
键 stud_id
(stud_id
),
约束 attend_ibfk_1
外键 (stud_id
) 引用 student
(id
)
) ENGINE=InnoDB AUTO_INCREMENT=11 默认字符集=latin1
插入数据-
插入 student
(id
, name
) 值 (1, 'hitesh'), (2, 'mundra'), (3, 'sumit'), (4, 'ashish');
插入 attend
(id
, stud_id
, date
) 值 (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;
我有以下查询
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`
这给了我以下结果:
我试过使用 GROUP BY student.StudentID
,但结果不正确。它在 'k1052280' 的“2015-09-14”列中显示 'Absent' 而不是现在。
我得到
我想得到这个结果
CREATE TABLE
student(
StudentIDvarchar(8) NOT NULL,
Namevarchar(100) NOT NULL,
Emailvarchar(254) CHARACTER SET latin1 NOT NULL,
WorkshopIDint(4) NOT NULL, PRIMARY KEY (
StudentID), UNIQUE KEY
StudentID(
StudentID,
Email), KEY
WorkshopID(
WorkshopID), CONSTRAINT
student_ibfk_1FOREIGN KEY (
WorkshopID) REFERENCES
workshop(
WorkshopID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE
attendance(
AttendanceIDint(10) NOT NULL AUTO_INCREMENT,
StudentIDvarchar(8) NOT NULL,
Datedate NOT NULL,
Timetime NOT NULL, PRIMARY KEY (
AttendanceID), UNIQUE KEY
unique_index(
StudentID,
Date), CONSTRAINT
attendance_ibfk_1FOREIGN KEY (
StudentID) REFERENCES
student(
StudentID) ) ENGINE=InnoDB AUTO_INCREMENT=194 DEFAULT CHARSET=utf8;
INSERT INTO
studentVALUES ('k1052280', 'Ali Shaikh', 'k1052280@something.com', '101'); INSERT INTO
studentVALUES ('k1052287', 'McKenzie Roth', 'Quisque@penatibus.edu', '102'); INSERT INTO
studentVALUES ('k1052288', 'Dacey Sullivan', 'sollicitudin.adipiscing.ligula@semmollisdui.com', '103'); INSERT INTO
studentVALUES ('k1052289', 'Callie Williamson', 'elementum@orciPhasellus.com', '104'); INSERT INTO
studentVALUES ('k1052290', 'Savannah Hyde', 'nec.metus.facilisis@nonummyut.co.uk', '101'); INSERT INTO
studentVALUES ('k1052291', 'Paul Tyson', 'semper.erat.in@ipsumleoelementum.net', '102'); INSERT INTO
studentVALUES ('k1052292', 'Nerea Ramos', 'gravida.sagittis.Duis@lacinia.edu', '103'); INSERT INTO
studentVALUES ('k1052293', 'Mark Mills', 'pellentesque.massa@blanditviverra.co.uk', '104'); INSERT INTO
studentVALUES ('k1052294', 'Zelda Cantu', 'ut@fringillaporttitorvulputate.org', '101');
INSERT INTO
attendanceVALUES ('168', 'k1052280', '2015-09-07', '00:00:00'); INSERT INTO
attendanceVALUES ('169', 'k1052287', '2015-09-09', '00:00:00'); INSERT INTO
attendanceVALUES ('170', 'k1052288', '2015-09-11', '00:00:00'); INSERT INTO
attendanceVALUES ('171', 'k1052289', '2015-09-11', '00:00:00'); INSERT INTO
attendanceVALUES ('172', 'k1052290', '2015-09-14', '00:00:00'); INSERT INTO
attendanceVALUES ('173', 'k1052291', '2015-09-16', '00:00:00'); INSERT INTO
attendanceVALUES ('174', 'k1052292', '2015-09-18', '00:00:00'); INSERT INTO
attendanceVALUES ('175', 'k1052293', '2015-09-18', '00:00:00'); INSERT INTO
attendanceVALUES ('176', 'k1052294', '2015-09-21', '00:00:00'); INSERT INTO
attendanceVALUES ('177', 'k1052295', '2015-09-23', '00:00:00'); INSERT INTO
attendanceVALUES ('178', 'k1052296', '2015-09-25', '00:00:00'); INSERT INTO
attendanceVALUES ('179', 'k1052297', '2015-09-25', '00:00:00'); INSERT INTO
attendanceVALUES ('183', 'k1052288', '2015-09-14', '00:00:00'); INSERT INTO
attendanceVALUES ('187', 'k1052290', '2015-09-07', '00:00:00'); INSERT INTO
attendanceVALUES ('188', 'k1052280', '2015-09-21', '10:30:00'); INSERT INTO
attendanceVALUES ('193', 'k1052280', '2015-04-05', '00:00:00');
这个查询可以工作-
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;
例子- 您可以像这样在列中使用子查询。
创建 Table: 创建 TABLE student
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(20) 默认为空,
主键 (id
)
) ENGINE=InnoDB AUTO_INCREMENT=8 默认字符集=latin1;
创建 Table: 创建 TABLE attend
(
id
int(11) NOT NULL AUTO_INCREMENT,
stud_id
int(11) 默认为空,
date
varchar(20) 默认为空,
主键(id
),
键 stud_id
(stud_id
),
约束 attend_ibfk_1
外键 (stud_id
) 引用 student
(id
)
) ENGINE=InnoDB AUTO_INCREMENT=11 默认字符集=latin1
插入数据-
插入 student
(id
, name
) 值 (1, 'hitesh'), (2, 'mundra'), (3, 'sumit'), (4, 'ashish');
插入 attend
(id
, stud_id
, date
) 值 (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;