MySQL Group By If子句
您好,我继承了一个使用MySQL作为数据库的学校出勤系统,管理员每周(计划)创建出勤记录,但在这一周,记录可能会被更改或删除(实际) 我需要显示两个表之间的所有不同记录(即实际v计划) 理想情况下,我会在“Actual”表中添加一个新列并跟踪所有已删除的记录,但不允许更改模式 我创建了一个MySQL,它显示了我用来获取不同记录的模式和查询 我的问题是我不明白如何合并两行并显示 在同一行中具有当天值的记录。如果我按学生分组,则只显示第一条记录 例如,如果在“[tblPlanned]”中存在某个学生的记录,但在“tblActual”中没有,那么我需要显示该记录。我还需要显示存在于[tblActual]中但不存在于[tblPlanned]中的记录 我真正想要的是以某种方式添加一个子句(优先级),如果学生的记录在当天的任何一个表中包含一个值,则显示它,否则显示null 下面是显示数据结构和所需输出的SQL FIDLE 任何提示都会非常有用MySQL Group By If子句,mysql,sql,Mysql,Sql,您好,我继承了一个使用MySQL作为数据库的学校出勤系统,管理员每周(计划)创建出勤记录,但在这一周,记录可能会被更改或删除(实际) 我需要显示两个表之间的所有不同记录(即实际v计划) 理想情况下,我会在“Actual”表中添加一个新列并跟踪所有已删除的记录,但不允许更改模式 我创建了一个MySQL,它显示了我用来获取不同记录的模式和查询 我的问题是我不明白如何合并两行并显示 在同一行中具有当天值的记录。如果我按学生分组,则只显示第一条记录 例如,如果在“[tblPlanned]”中存在某个学生
SELECT * FROM (
(SELECT a.Classroom as classroom, a.Student as student,
MAX(case (a.DropDate) when '20160222' then a.IsAbsent else ' ' end) as 'day_1',
MAX(case (a.DropDate) when '20160223' then a.IsAbsent else ' ' end ) as 'day_2',
MAX(case (a.DropDate) when '20160224' then a.IsAbsent else ' ' end ) as 'day_3'
FROM Attendance a
WHERE a.DropDate IN ('20160222','20160223','20160224') AND a.classroom = '17' AND
NOT EXISTS( SELECT 1
FROM Staging AS p
WHERE p.Student = a.Student AND
p.IsAbsent = a.IsAbsent AND
p.DropDate = a.DropDate
)
)
UNION
(SELECT t.Classroom as classroom, t.Student as student,
MAX(case (t.DropDate) when '20160222' then t.IsAbsent else ' ' end ) as 'day_1',
MAX(case (t.DropDate) when '20160223' then t.IsAbsent else ' ' end ) as 'day_2',
MAX(case (t.DropDate) when '20160224' then t.IsAbsent else ' ' end ) as 'day_3'
FROM Staging t
WHERE t.DropDate IN ('20160222','20160223','20160224') AND t.classroom = '17'AND
NOT EXISTS( SELECT 1
FROM Attendance AS u
WHERE u.Student = t.Student AND
u.IsAbsent = t.IsAbsent AND
u.DropDate = t.DropDate
)
)
) tbl ORDER BY classroom, student
你在找这样的东西吗?
LEFT JOIN
或RIGHT JOIN
子查询取决于您希望哪个子查询具有优先级,然后使用IFNULL
函数
SELECT tbl2.classroom, tbl2.student, IFNULL(tbl2.day_1, tbl1.day_1) day_1
,IFNULL(tbl2.day_2, tbl1.day_2) day_2, IFNULL(tbl2.day_3, tbl1.day_3) day_3
FROM
((SELECT
a.Classroom as classroom,
a.Student as student,
MAX( case (a.DropDate)
when '20160222'
then a.IsAbsent
else ' ' end ) as 'day_1',
MAX( case (a.DropDate)
when '20160223'
then a.IsAbsent
else ' ' end ) as 'day_2',
MAX( case (a.DropDate)
when '20160224'
then a.IsAbsent
else ' ' end ) as 'day_3'
FROM Attendance a
WHERE a.DropDate IN ('20160222','20160223','20160224')
AND a.classroom = '17'
AND NOT EXISTS
( SELECT 1
FROM Staging AS p
WHERE p.Student = a.Student
AND p.IsAbsent = a.IsAbsent
AND p.DropDate = a.DropDate
)
) as tbl1
RIGHT JOIN
(SELECT
t.Classroom as classroom,
t.Student as student,
MAX( case (t.DropDate)
when '20160222'
then t.IsAbsent
else ' ' end ) as 'day_1',
MAX( case (t.DropDate)
when '20160223'
then t.IsAbsent
else ' ' end ) as 'day_2',
MAX( case (t.DropDate)
when '20160224'
then t.IsAbsent
else ' ' end ) as 'day_3'
FROM Staging t
WHERE t.DropDate IN ('20160222','20160223','20160224')
AND t.classroom = '17'
AND NOT EXISTS
( SELECT 1
FROM Attendance AS u
WHERE u.Student = t.Student
AND u.IsAbsent = t.IsAbsent
AND u.DropDate = t.DropDate
)
)tbl2 ON tbl1.classroom = tbl2.classroom and tbl1.student = tbl2.student)
ORDER BY classroom, student
您能在实际问题中向我们展示您的原始表格吗?谢谢@TimBiegeleisen我的模式在这里可用。您能更好地描述您正在寻找的列和条件的确切输出吗?谢谢,这太完美了!!有没有办法确定它来自哪个表。例如,我可以在day_1或day_2列中显示它是tbl1还是tbl2吗?再次感谢您可以将其更改为
完全联接
并选择所有字段:选择…,tbl1.day_1,tbl2.day_1,tbl1.day_2,tbl2.day_2,…
,然后您就知道哪个表是空的,哪个表不是空的。