mysql - 多行数据作为单个查询结果 - My SQL
问题描述
需要将多个行值作为单个结果获取。
餐桌人员
staffID, staffname
1 John
2 Smith
课程表
courseId, staffId, courseId, courseDesc
1 1 3 Chemistry
2 1 4 Physics
3 1 5 Math
4 2 6 Science
预期成绩:
staffId, staffName, isScience, isMath, isPhyscis, isChemistry
1 John false true true true
解决方案
您可以加入和使用条件聚合:
select
s.id,
s.name,
max(coursedesc = 'Science' ) isScience,
max(coursedesc = 'Math' ) isMath,
max(coursedesc = 'Physics' ) isPhysics,
max(coursedesc = 'Chemistry') isChemistry
from staff s
left join course c on c.staffid = s.id
group by s.id, s.name
这会将0
/1
值放在附加列中,这与 MySQL 中的布尔值非常接近。
推荐阅读
- javascript - 正则表达式:按字符拆分字符串,除非在引号或双引号内
- java - 我的 setText 中是否必须有一个字符串值?
- python - 将多标签转换为单标签问题
- javascript - React Native,意外的关键字“This”
- scala - 为什么并行聚合在火花中没有更快?
- python-3.x - 在终端和变量之间拆分标准输出
- ssl - ESP32 MQTT TLS 错误(自签名证书)
- python - 如何在打开自动重载的情况下在 VSCode 中调试 Django
- list - 数据框列中的嵌套列表,提取数据框列中列表的值 Pyspark Spark
- android - ACTION_SEND_MULTIPLE mime 类型过滤器似乎完全损坏