【问题标题】:Remove Duplicates from multi case query in MySQL从 MySQL 中的多案例查询中删除重复项
【发布时间】:2015-06-11 18:45:58
【问题描述】:

我有以下查询

 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”列中显示“缺席”,而不是当前。

我来了

我想得到这个结果

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_1FOREIGN KEY (WorkshopID) REFERENCESworkshop(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 ,日期), CONSTRAINT attendance_ibfk_1 FOREIGN KEY ( StudentID ) REFERENCES student(StudentID) ) ENGINE=InnoDB AUTO_INCREMENT=194 DEFAULT CHARSET=utf8;

INSERT INTOstudentVALUES ('k1052280', 'Ali Shaikh', 'k1052280@something.com', '101'); INSERT INTOstudentVALUES ('k1052287', 'McKenzie Roth', 'Quisque@penatibus.edu', '102'); INSERT INTOstudentVALUES ('k1052288', 'Dacey Sullivan', 'sollicitudin.adipiscing.ligula@semmollisdui.com', '103'); INSERT INTOstudentVALUES ('k1052289', 'Callie Williamson', 'elementum@orciPhasellus.com', '104'); INSERT INTOstudentVALUES ('k1052290', 'Savannah Hyde', 'nec.metus.facilisis@nonummyut.co.uk', '101'); INSERT INTOstudentVALUES ('k1052291', 'Paul Tyson', 'semper.erat.in@ipsumleoelementum.net', '102'); INSERT INTOstudentVALUES ('k1052292', 'Nerea Ramos', 'gravida.sagittis.Duis@lacinia.edu', '103'); INSERT INTOstudentVALUES ('k1052293', 'Mark Mills', 'pellentesque.massa@blanditviverra.co.uk', '104'); INSERT INTOstudentVALUES ('k1052293', 'Mark Mills', 'pellentesque.massa@blanditviverra.co.uk', '104'); INSERT INTOstudent@76

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出勤VALUES ('177', 'k1052295', '2015-09-23', '00:00:00'); INSERT INTO出勤VALUES ('178', 'k1052296', '2015-09-25', '00:00:00'); INSERT INTO出勤VALUES ('179', 'k1052297', '2015-09-25', '00:00:00'); INSERT INTOattendanceVALUES ('183', 'k1052288', '2015-09-14', '00:00:00'); INSERT INTOattendanceVALUES ('187', 'k1052290', '2015-09-07', '00:00:00'); INSERT INTOattendanceVALUES ('188', 'k1052280', '2015-09-21', '10:30:00'); INSERT INTOattendanceVALUES ('193', 'k1052280', '2015-04-05', '00:00:00');

【问题讨论】:

  • 你想看到什么结果?
  • @TimBiegeleisen 我想看看他们是否在场,如果他们在每个日期都缺席,我想看缺席
  • 你还没有给我们一个规则来告诉我们如何在k1052280的两行之间进行选择。请解释你的逻辑。
  • 学生“Savannah”如何同时“缺席”和“在场”?你的逻辑有问题。
  • 表格错了,一个人不应该有多个条目,我认为您需要更改初始查询,这样每个人只会返回一行

标签: php mysql mysqli duplicates


【解决方案1】:

这个查询可以工作-

 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) 默认为空, 主键 (id) ) ENGINE=InnoDB AUTO_INCREMENT=8 默认字符集=latin1;

创建表:CREATE 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;

【讨论】:

  • 我修改为将它与我的数据库一起使用,我收到此错误 [Err] 1242 - 子查询返回超过 1 行
  • 使用这个查询--- 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')作为学生作为 s 的“06-04-2015”;
  • 我试过这个,但我遇到了另一个问题,查询没有显示正确的结果,第一列存在,第二列不存在所有记录。我使用的查询 --- 'SELECT Student.StudentID, Student.NAME, IF (( SELECT distinct 1 FROM admission WHERE admission.StudentID = StudentID AND date = '2015-04-05' ) = 1, 'Present', '缺席' ) AS '05-04-2015', IF ( ( SELECT distinct 1 FROM Attendance WHERE admission.StudentID = StudentID AND date = '2015-04-7' ) = 1, 'Present', 'Absent' ) AS '06-04-2015' FROM student as student'
  • 检查第二列中的日期值。这不匹配任何值,我的意思是条件不满足。在内部子查询中写入适当的条件。我已经测试过了,它运行良好。
  • 我更改了日期,但问题是如果一个人符合条件,则让每个人都在场,从而使每个人都出现在日期列中
猜你喜欢
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 2012-11-02
  • 2018-10-26
  • 1970-01-01
相关资源
最近更新 更多