MySql 中的 WHEN 条件生成多列而不是一列?
WHEN condition in MySql genrating multiple column instead of one?
我有两个表格(学生和分数),如下图所示:
学生 TABLE:
标记 TABLE:
查询:
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
CASE WHEN m.math < 30 THEN 'Fail in Math' else '' END,
CASE WHEN m.English < 30 THEN 'Fail in English' else '' END,
CASE WHEN m.science < 30 THEN 'Fail in science' else '' END,
CASE WHEN m.Hindi < 30 THEN 'Fail in Hindi' else '' END,
CASE WHEN m.s_science < 30 THEN 'Fail in s_science' else '' END
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
结果:
预期输出:列备注将创建一次,最后的备注将在单列中。
您的查询中存在语法问题。试试这个。
如果你只想显示一个备注。
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
CASE WHEN m.math < 30 THEN 'Fail in Math'
WHEN m.English < 30 THEN 'Fail in English'
WHEN m.science < 30 THEN 'Fail in science'
WHEN m.Hindi < 30 THEN 'Fail in Hindi'
WHEN m.s_science < 30 THEN 'Fail in s_science' else '' END
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
否则,如果您想合并列中的所有备注,请使用 concat
。由于有多个主题标记。
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
Concat( CASE WHEN m.math < 30 THEN 'Fail in Math' else '' END,
CASE WHEN m.English < 30 THEN 'Fail in English' else '' END,
CASE WHEN m.science < 30 THEN 'Fail in science' else '' END,
CASE WHEN m.Hindi < 30 THEN 'Fail in Hindi' else '' END,
CASE WHEN m.s_science < 30 THEN 'Fail in s_science' else '' END
)
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
您可以尝试使用 group_concat 来根据子查询
进行备注
select s.*, t.Total, group_concat(t.remark), t.result
from join student as s
INNER JOIN (
select s.st_id, m.math+m.English+m.science+m.hindi+m.s_science as Total,
CASE WHEN m.math < 30 THEN 'Fail in Math' else '' END,
CASE WHEN m.English < 30 THEN 'Fail in English' else '' END,
CASE WHEN m.science < 30 THEN 'Fail in science' else '' END,
CASE WHEN m.Hindi < 30 THEN 'Fail in Hindi' else '' END,
CASE WHEN m.s_science < 30 THEN 'Fail in s_science' else '' END
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
) t on t.st_id = s.st_id
GROUP BY s.st_id
你可以用 concat()
:
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
trim(trailing ',' from concat(
CASE WHEN m.math < 30 THEN 'Fail in Math,' else '' END,
CASE WHEN m.English < 30 THEN 'Fail in English,' else '' END,
CASE WHEN m.science < 30 THEN 'Fail in science,' else '' END,
CASE WHEN m.Hindi < 30 THEN 'Fail in Hindi,' else '' END,
CASE WHEN m.s_science < 30 THEN 'Fail in s_science,' else '' END
)) AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
像在单个案例中一样尝试这个
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
CASE
WHEN m.math < 30 THEN 'Fail in Math'
WHEN m.English < 30 THEN 'Fail in English'
WHEN m.science < 30 THEN 'Fail in science'
WHEN m.Hindi < 30 THEN 'Fail in Hindi'
WHEN m.s_science < 30 THEN 'Fail in s_science'
END
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
这可能只是评论部分对您的请求的评论,但对于评论来说太长了...
最好将数据库设计更改为三个表:
- 学生(student_id,姓名,...)
- class(class_id,姓名,...)
- student_class (student_id, class_id, 标记)
因此,您不必了解查询中的特定主题,也不必在每次删除或添加主题时更改所有查询。
select *
from student
join
(
select
sc.student_id,
sum(sc.mark) as total,
group_concat(case when sc.mark < 30 then c.name end
order by c.name separator ', ') as failed_classes,
sum(sc.mark) < 150 as failed
from student_class sc
join class c on c.class_id = sc.class_id
group by sc.student_id
) evaluated using (student_id)
order by student_id;
我有两个表格(学生和分数),如下图所示:
学生 TABLE:
标记 TABLE:
查询:
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
CASE WHEN m.math < 30 THEN 'Fail in Math' else '' END,
CASE WHEN m.English < 30 THEN 'Fail in English' else '' END,
CASE WHEN m.science < 30 THEN 'Fail in science' else '' END,
CASE WHEN m.Hindi < 30 THEN 'Fail in Hindi' else '' END,
CASE WHEN m.s_science < 30 THEN 'Fail in s_science' else '' END
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
结果:
预期输出:列备注将创建一次,最后的备注将在单列中。
您的查询中存在语法问题。试试这个。
如果你只想显示一个备注。
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
CASE WHEN m.math < 30 THEN 'Fail in Math'
WHEN m.English < 30 THEN 'Fail in English'
WHEN m.science < 30 THEN 'Fail in science'
WHEN m.Hindi < 30 THEN 'Fail in Hindi'
WHEN m.s_science < 30 THEN 'Fail in s_science' else '' END
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
否则,如果您想合并列中的所有备注,请使用 concat
。由于有多个主题标记。
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
Concat( CASE WHEN m.math < 30 THEN 'Fail in Math' else '' END,
CASE WHEN m.English < 30 THEN 'Fail in English' else '' END,
CASE WHEN m.science < 30 THEN 'Fail in science' else '' END,
CASE WHEN m.Hindi < 30 THEN 'Fail in Hindi' else '' END,
CASE WHEN m.s_science < 30 THEN 'Fail in s_science' else '' END
)
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
您可以尝试使用 group_concat 来根据子查询
进行备注 select s.*, t.Total, group_concat(t.remark), t.result
from join student as s
INNER JOIN (
select s.st_id, m.math+m.English+m.science+m.hindi+m.s_science as Total,
CASE WHEN m.math < 30 THEN 'Fail in Math' else '' END,
CASE WHEN m.English < 30 THEN 'Fail in English' else '' END,
CASE WHEN m.science < 30 THEN 'Fail in science' else '' END,
CASE WHEN m.Hindi < 30 THEN 'Fail in Hindi' else '' END,
CASE WHEN m.s_science < 30 THEN 'Fail in s_science' else '' END
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
) t on t.st_id = s.st_id
GROUP BY s.st_id
你可以用 concat()
:
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
trim(trailing ',' from concat(
CASE WHEN m.math < 30 THEN 'Fail in Math,' else '' END,
CASE WHEN m.English < 30 THEN 'Fail in English,' else '' END,
CASE WHEN m.science < 30 THEN 'Fail in science,' else '' END,
CASE WHEN m.Hindi < 30 THEN 'Fail in Hindi,' else '' END,
CASE WHEN m.s_science < 30 THEN 'Fail in s_science,' else '' END
)) AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
像在单个案例中一样尝试这个
select s.*, m.math+m.English+m.science+m.hindi+m.s_science as Total,
CASE
WHEN m.math < 30 THEN 'Fail in Math'
WHEN m.English < 30 THEN 'Fail in English'
WHEN m.science < 30 THEN 'Fail in science'
WHEN m.Hindi < 30 THEN 'Fail in Hindi'
WHEN m.s_science < 30 THEN 'Fail in s_science'
END
AS Remark,
CASE WHEN (m.math+m.English+m.science+m.hindi+m.s_science) < 150 THEN 'Fail' else 'Pass' END AS Result
from marks as m
join student as s
on m.st_id = s.st_id
这可能只是评论部分对您的请求的评论,但对于评论来说太长了...
最好将数据库设计更改为三个表:
- 学生(student_id,姓名,...)
- class(class_id,姓名,...)
- student_class (student_id, class_id, 标记)
因此,您不必了解查询中的特定主题,也不必在每次删除或添加主题时更改所有查询。
select *
from student
join
(
select
sc.student_id,
sum(sc.mark) as total,
group_concat(case when sc.mark < 30 then c.name end
order by c.name separator ', ') as failed_classes,
sum(sc.mark) < 150 as failed
from student_class sc
join class c on c.class_id = sc.class_id
group by sc.student_id
) evaluated using (student_id)
order by student_id;