我有以下查询
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
但是它给了我不正确的结果。它在'2015-09-14'栏中显示'缺席'为'k1052280'而非现在。
我正在
我希望得到这个结果
CREATE TABLE
学生(
StudentIDvarchar(8) NOT NULL,
名称varchar(100) NOT NULL,
电子邮件varchar(254) CHARACTER SET latin1 NOT NULL,
WorkshopIDint(4) NOT NULL, PRIMARY KEY (
StudentID), UNIQUE KEY
StudentID(
StudentID,
电子邮件), KEY
WorkshopID(
WorkshopID), CONSTRAINT
student_ibfk_1FOREIGN KEY (
WorkshopID) REFERENCES
车间(
WorkshopID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE
出勤(
AttendanceIDint(10) NOT NULL AUTO_INCREMENT,
StudentIDvarchar(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}}
答案 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_id
(stud_id
),
约束attend_ibfk_1
外键(stud_id
)参考student
(id
)
)ENGINE = InnoDB AUTO_INCREMENT = 11 DEFAULT CHARSET = latin1
插入数据 -
INSERT INTO student
(id
,name
)VALUES(1,' hitesh'),(2,' mundra'), (3,' sumit'),(4,' ashish');
INSERT INTO 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;